File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes String array to arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
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: 1296
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: 24183
    
  34

Otherwise, there's always

new ArrayList(Arrays.asList(myArray));


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

Joined: Jul 15, 2003
Posts: 1078
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: 47
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.

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: 47
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: 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


"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: 38007
    
  22
Welcome to the Ranch colin wang
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

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
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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 - OCEEJBD 6
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: 38007
    
  22
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: 50

Using asList() method is the best way


Regards,

RK
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3817

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
C Bryant
Greenhorn

Joined: Mar 29, 2012
Posts: 6
But, not the following?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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

Joined: Mar 29, 2012
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

Joined: Jan 03, 2004
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

Joined: Mar 29, 2012
Posts: 6
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!!
dennis deems
Ranch Hand

Joined: Mar 12, 2011
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

Joined: Mar 12, 2011
Posts: 808
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.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3429
    
  12
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.


Joanne
C Bryant
Greenhorn

Joined: Mar 29, 2012
Posts: 6
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

Joined: Aug 22, 2010
Posts: 3606
    
  60

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

Joined: Mar 29, 2012
Posts: 6
later dude. work to do.
B Verma
Greenhorn

Joined: Jun 07, 2012
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

Joined: Oct 13, 2005
Posts: 38007
    
  22
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

Joined: Jun 07, 2012
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.
 
Consider Paul's rocket mass heater.
 
subject: String array to arraylist
 
Similar Threads
int[] = null; help
return string array
Date difference, with the table containing null values for date
Convert ListArray to string array
casting doubt