Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!

# String array to arraylist

vani mn
Greenhorn
Posts: 1
• 3
How to convert a string array to arraylist??

Garrett Rowe
Ranch Hand
Posts: 1296
• 11
If the concrete implementation doesn't matter, you can use Arrays.asList() to convert your array to a List.

Ernest Friedman-Hill
author and iconoclast
Marshal
Posts: 24212
35
• 10
Otherwise, there's always

new ArrayList(Arrays.asList(myArray));

Ken Blair
Ranch Hand
Posts: 1078
• 3
Or

Janarthan S Sathiamurthy
Greenhorn
Posts: 4
• 9
import java.util.Collections;

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

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

Best regards,
Janarthan S

Ziyang Zhang
Ranch Hand
Posts: 47
• 5
Janarthan S Sathiamurthy wrote:import java.util.Collections;

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

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

Best regards,
Janarthan S

Good solution!!!

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15634
46
• 32
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.

Ziyang Zhang
Ranch Hand
Posts: 47
• 1
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.

But I think in most conditions, people will want to make a "static" array to a "dynamic" arraylist.

Hunter McMillen
Ranch Hand
Posts: 492
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

Greenhorn
Posts: 12
• 1
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

Colin Wang
Greenhorn
Posts: 4
• 2
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
Posts: 51407
87
Welcome to the Ranch colin wang

Kaustubh G Sharma
Ranch Hand
Posts: 1283
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

anbu selvan
Greenhorn
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();

Rob Spoor
Sheriff
Posts: 20751
68
Anbu, welcome to the Ranch!

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

Ming Wilson
Greenhorn
Posts: 3
• 1
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
Posts: 51407
87
Welcome to the Ranch Ming Wilson

Ming Wilson
Greenhorn
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
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.

Ranch Hand
Posts: 63
Using asList() method is the best way

Vijitha Kumara
Bartender
Posts: 3930
21
And welcome to CodeRanch, Vinay Mr!

chan nyeinkyaw
Greenhorn
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.

Thanks

C Bryant
Greenhorn
Posts: 6
But, not the following?

Rob Spoor
Sheriff
Posts: 20751
68
• 1
Although that does return an ArrayList, it's not java.util.ArrayList. It's java.util.Collections.ArrayList, a private nested class that implements List. And the question was to convert into an ArrayList.

By the way, your code won't compile. You can't create an array like that, you can only use this shorthand way when you initialize an array immediately when you declare it.

C Bryant
Greenhorn
Posts: 6
Yes well, that was sort of the point of the example. The code looks like it should work to me. It makes logical sense syntactically.

Why should there be a two step process? 1. Create the String array. 2. Convert the array to a list? Why can't I do this all in one statement?

Jeff Verdegan
Bartender
Posts: 6109
6
C Bryant wrote:Yes well, that was sort of the point of the example. The code looks like it should work to me. It makes logical sense syntactically.

Why should there be a two step process? 1. Create the String array. 2. Convert the array to a list? Why can't I do this all in one statement?

You can do it in one statement:

Perhaps you mean something like this?

If that's your question, then the answer is: Because arrays are part of the Java language and as such, they get special language-level treatment (kind of like Strings do). The Collections Framework (including ArrayList), is not part of the language. It's just part of the API. As far as the compiler is concerned, it's no different than any class you or I might write.

C Bryant
Greenhorn
Posts: 6
Sure, that's cool, and simpler, too. Thanks!

Only the second one doesn't compile?

01. Compiles, albeit with an "unchecked conversion" warning.
02. Does not compile.
03. Cut and pasted from the JavaDocs 1.6 API does not compile.

What gives? I want to assign to a List, because I do not want to write to implementation classes. I want to code to interfaces. Stooges, indeed!!

dennis deems
Ranch Hand
Posts: 808
C Bryant wrote:But, not the following?

To make this work you have to use the new keyword:

(It's still not the expected implementation, as mentioned above.)

dennis deems
Ranch Hand
Posts: 808
C Bryant wrote:Sure, that's cool, and simpler, too. Thanks!

Only the second one doesn't compile?

01. Compiles, albeit with an "unchecked conversion" warning.
02. Does not compile.
03. Cut and pasted from the JavaDocs 1.6 API does not compile.

What gives? I want to assign to a List, because I do not want to write to implementation classes. I want to code to interfaces. Stooges, indeed!!

What Java version are you using? The third example works fine for me. And I don't get a warning from the first one. I'm not sure why the second example would be expected to work.

Joanne Neal
Rancher
Posts: 3742
16
C Bryant wrote:

03. Cut and pasted from the JavaDocs 1.6 API does not compile.

What error do you get for 3. Works fine for me.

Edit: Too slow. Shouldn't have spent time stripping out the superfluous stuff from the quote.

C Bryant
Greenhorn
Posts: 6
• 1
Eclipse Indigo running Java 1.6 with Java 1.5 compliance on a Mac.

Seems like we are splitting hairs. The syntax shouldn't be that fragile.

So, if I stand on my head, and touch my right elbow, and hold down the Shift key . . . .

Martin Vajsar
Sheriff
Posts: 3752
62
C Bryant wrote:Eclipse Indigo running Java 1.6 with Java 1.5 compliance on a Mac.

Seems like we are splitting hairs. The syntax shouldn't be that fragile.

So, if I stand on my head, and touch my right elbow, and hold down the Shift key . . . .

Umm, that's way strange. What exact message do you get from the compiler? Could you post full source code and full compiler output?

C Bryant
Greenhorn
Posts: 6
later dude. work to do.

B Verma
Greenhorn
Posts: 7
You can use org.apache.axis2.databinding.utils.ConverterUtil.toList(Object[]) to convert any array to a list. The generated list will be independent of the array, means you can add, delete or modify any element in the list without affecting the array.

Example:-

String[] strArray = {"data1","data2","data3","data4","data5"};
List list = ConverterUtil.toList(strArray);

Hope this helps

Campbell Ritchie
Sheriff
Posts: 51407
87
• 1
Welcome to the Ranch

I wasn’t aware of that class; there are all sorts of useful things hiding away in the Apache project.

B Verma
Greenhorn
Posts: 7
Campbell Ritchie wrote:Welcome to the Ranch

I wasn’t aware of that class; there are all sorts of useful things hiding away in the Apache project.

Thanks Campbell, yes indeed these Apache classes are having lot of useful utilities worth exploring.

 It is sorta covered in the JavaRanch Style Guide.