• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generic method that can accpet ArrayList<E> OR ArrayList<E[]> as its parameter  RSS feed

 
Sina Meraji
Greenhorn
Posts: 13
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: Is there a way to call this method by passing either an ArrayList<String> OR an ArrayList<String[]> to its parameter?



(are generic wildcard relevant to this problem?)
thanks
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sina Meraji wrote:Question: Is there a way to call this method by passing either an ArrayList<String> OR an ArrayList<String[]> to its parameter?



(are generic wildcard relevant to this problem?)
thanks


There ya go, but why you would want to is beyond me.
 
Sina Meraji
Greenhorn
Posts: 13
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing a program where we keep reading from txt files into arrays, sometimes ArrayList<String> and sometimes ArrayList<String[]>

thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sina Meraji wrote:Writing a program where we keep reading from txt files into arrays, sometimes ArrayList<String> and sometimes ArrayList<String[]>

Why? Surely an ArrayList<String[]> covers both cases. If you get a String rather than a String[], just call:

  myList.add(new String[] { singleString });

I suspect you're overthinking this...

Winston
 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Winston.

Les, you should never use raw types. Even if there was a valid reason to do what the OP wanted, you should use ArrayList<Object> rather than a raw ArrayList. You should also use the diamond operator while instantiating generic types.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!