• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array to ArrayList

 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have an array of Strings eg: String[] names. I would like to convert this to an ArrayList. I can write code to do it. But does anyone know of a method in the java API which already does this?

Thanks,
Cathy.
 
Weronpc
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Cathy,

private static final String names[] = {"Mike", "Cathy", "Nancy"};

List nome = new ArrayList( Arrays.asList( names ) );

By the way Cathy, I didn't test it out, but should work.

Mike
[ June 15, 2004: Message edited by: mike chi ]
 
Cathy Song
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike.
 
Leo Deegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should work. In fact, you can do this:

 
Junilu Lacar
Bartender
Pie
Posts: 7747
60
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B]That should work. In fact, you can do this:

[/B]

Yes, but Cathy wanted an ArrayList specifically. There's no guarantee that Arrays.asList() will return an instance of ArrayList. Your code is preferable though if there is no requirement for a specific type, just that it implements the List interface.
 
Leo Deegan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, yes, good eye. I missed that detail in the question.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic