The moose likes Java in General and the fly likes String array to arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "String array to arraylist" Watch "String array to arraylist" New topic
Author

String array to arraylist

vani mn
Greenhorn

Joined: Jan 21, 2006
Posts: 1
How to convert a string array to arraylist??
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
If the concrete implementation doesn't matter, you can use Arrays.asList() to convert your array to a List.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 23240

Otherwise, there's always

new ArrayList(Arrays.asList(myArray));


[Jess in Action][AskingGoodQuestions]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1077
Or

Janarthan S Sathiamurthy
Greenhorn

Joined: Oct 30, 2007
Posts: 4
import java.util.Collections;

List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};

Collections.addAll(myList, myStringArray);

After this code, 'myList' should contain all the elements from the array.

Best regards,
Janarthan S
Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 45
Janarthan S Sathiamurthy wrote:import java.util.Collections;

List myList = new ArrayList();
String[] myStringArray = new String[] {"Java", "is", "Cool"};

Collections.addAll(myList, myStringArray);

After this code, 'myList' should contain all the elements from the array.

Best regards,
Janarthan S


Good solution!!!


Enjoy life!
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 11185

There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.

This message was edited 2 times. Last update was at by Jesper Young


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ziyang Zhang
Ranch Hand

Joined: Jul 17, 2010
Posts: 45
Jesper Young wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.


These are great comments.
But I think in most conditions, people will want to make a "static" array to a "dynamic" arraylist.
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 476

You have to be careful with the kind of terminology you use. "static" is a keyword in Java that has a particular meaning. By saying "static" array I think you mean "fixed-size" array. A "static" array in Java is an array that belongs to a particular instance of a class.

Hunter


"If the facts don't fit the theory, get new facts" --Albert Einstein
Muhammad Safwat
Greenhorn

Joined: Aug 12, 2008
Posts: 12
Jesper Young wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.

Very good analysis and you need to add also that copying the array affects the performance of your code so if performance is an issue then user the Arrays.asList() way


http://www.thejavacode.com/
Colin Wang
Greenhorn

Joined: Nov 24, 2010
Posts: 4
Perfect comments.
Thank you.
Jesper de Jong wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 24962
Welcome to the Ranch colin wang
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1033

Jesper de Jong wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.


Jesper you really rocks man...Wonderful explanation...keep up the good work


No Kaustubh No Fun, Know Kaustubh Know Fun..
anbu selvan
Greenhorn

Joined: Jan 13, 2011
Posts: 1
we can add array of string to List

it is one more option to add string to list

String na ={"anbu","selvan","raja"};
ArrayList ar = new ArraList();
List list = Collections.addAll(ar,na);
Rob Spoor
Saloon Keeper

Joined: Oct 27, 2005
Posts: 17194

Anbu, welcome to the Ranch!

Your code won't compile - Collections.addAll returns a boolean, not a List.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Ming Wilson
Greenhorn

Joined: Apr 29, 2011
Posts: 3
Kaustubh G Sharma wrote:
Jesper de Jong wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.


Jesper you really rocks man...Wonderful explanation...keep up the good work


Excellent comment.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 24962
Welcome to the Ranch Ming Wilson
Ming Wilson
Greenhorn

Joined: Apr 29, 2011
Posts: 3
Campbell Ritchie wrote:Welcome to the Ranch Ming Wilson


Thank you very much, I have been referred to this site a 1000's of times in relations to problems that I have googled and have always found the responses of similar problems to be helpful, inciteful and non degrading of the developer, but I do love the mentioning of best practices.

I look forward to our mutual journey through Java together.
Vinay M Raju
Greenhorn

Joined: Aug 01, 2011
Posts: 3
Jesper de Jong wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.



Excellent comment. These are the pearls of wisdom we look forward to in this forum. Thanks a lot.
rk sharma
Ranch Hand

Joined: Jun 25, 2011
Posts: 49

Using asList() method is the best way


Regards,

RK
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3355

And welcome to CodeRanch, Vinay Mr!


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
chan nyeinkyaw
Greenhorn

Joined: Oct 19, 2011
Posts: 1

Jesper de Jong wrote:There are some important things to note with the solutions given above:

Garrett's solution, with Arrays.asList() is efficient because it doesn't need to copy the content of the array. This method returns a List that is a "view" onto the array - a wrapper that makes the array look like a list. When you change an element in the list, the element in the original array is also changed. Note that the list is fixed size - if you try to add elements to the list, you'll get an exception.

Ernest's solution: new ArrayList(Arrays.asList(myArray)); copies the content of the array to a new ArrayList. The copy is ofcourse independent of the array, and you can add, remove etc. elements as you like.

Janarthan's solution, with Collections.addAll(myList, myStringArray); is essentially the same as Ernest's solution.

If you only need read access to the array as if it is a List and you don't want to add or remove elements from the list, then use Garrett's solution. Otherwise use Ernest's or Janarthan's solution.


nice comments
Thanks


whatever can go wrong, will go wrong
 
 
subject: String array to arraylist
 
MyEclipse, The Clear Choice