Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

String array to arraylist  RSS feed

 
Greenhorn
Posts: 1
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to convert a string array to arraylist??
 
Ranch Hand
Posts: 1296
  • Likes 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the concrete implementation doesn't matter, you can use Arrays.asList() to convert your array to a List.
 
author and iconoclast
Posts: 24203
40
Chrome Eclipse IDE Mac OS X
  • Likes 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Otherwise, there's always

new ArrayList(Arrays.asList(myArray));
 
Ranch Hand
Posts: 1078
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or

 
Greenhorn
Posts: 4
  • Likes 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 47
  • Likes 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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!!!
 
Java Cowboy
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Likes 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 4
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 61697
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch colin wang
 
Ranch Hand
Posts: 1283
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Sheriff
Posts: 21463
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anbu, welcome to the Ranch!

Your code won't compile - Collections.addAll returns a boolean, not a List.
 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 61697
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Ming Wilson
 
Ming Wilson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Google App Engine Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using asList() method is the best way
 
Bartender
Posts: 4089
54
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to CodeRanch, Vinay Mr!
 
Greenhorn
Posts: 1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, not the following?

 
Rob Spoor
Sheriff
Posts: 21463
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, that's cool, and simpler, too. Thanks!

Only the second one doesn't compile?

How about these three?



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!!
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

C Bryant wrote:Sure, that's cool, and simpler, too. Thanks!

Only the second one doesn't compile?

How about these three?



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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 . . . .
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
later dude. work to do.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 61697
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!