Overloading functions: is there anyway to overload functions if they take arraylists?

the following fails at compile time and says they’res a duplicate function,

void saveData(ArrayList<String> a){
    if(click){
      for(int i=0;i<a.size();i++)
        output.println(a.get(i));
        saveTo();
    }
  };
  
  void saveData(ArrayList<Float> a){
    if(click){
      for(int i=0;i<a.size();i++)
        output.println(a.get(i));
        saveTo();
    }
  };

however the following works no problem,

void saveData(String []a){
    if(click){
      for(int i=0;i<a.length;i++)
        output.println(a[i]);
        saveTo();
    }
  };
  
  void saveData(float []a){
    if(click){
      for(int i=0;i<a.length;i++)
        output.println(a[i]);
        saveTo();
    }
  };

I’m thinking this is probably due to the logic of the logic of processing, but am I perhaps missing something? This isn’t really and issue as I can simply process the information outside the file saving function but as it threw an error I was just intrigued.

1 Like

I think it has something to do with type erasure. I found this article and it looks like instanceof is a possible solution.

https://wiki.sei.cmu.edu/confluence/plugins/servlet/mobile?contentId=88487555#content/view/88487555

2 Likes