This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "String array to arraylist" Watch "String array to arraylist" New topic
Author

String array to arraylist

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4446
    
    5

B Verma wrote:
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.


Apache classes are useful - true that. However, I would think twice before using ConverterUtil.toList over some of the ways suggested before that use standard Java. ConverterUtil smells of NIH (Not Invented Here). I may be wrong and there may be perfectly legitimate reasons for its existence in the Apache project but I would question the need to suddenly create a dependency on Apache Axis just because you want to convert an array to a List. That's a bit of a stretch for an old guy like me who can't even reach his toes without bending his knees anymore.


Junilu - [How to Ask Questions] [How to Answer Questions]
B Verma
Greenhorn

Joined: Jun 07, 2012
Posts: 7
I do agree with you Junilu.
caushik conjetty sekhar
Greenhorn

Joined: Mar 03, 2009
Posts: 4
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



very good thread!
Sumit Kayat
Greenhorn

Joined: Dec 23, 2012
Posts: 3
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



Its Not Complied ... 2 Notes Shown
1 java Uses unchecked and Unsafe Opeations
2Recompile with Xlint
help out
Harinder Bedi
Greenhorn

Joined: Feb 16, 2013
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 for the info.
diksha singh neel
Greenhorn

Joined: Jul 28, 2013
Posts: 10
Declare an arraylist, take the size of the array, loop that many times and enter elements one by one into the arraylist.
Scott Shipp
Ranch Hand

Joined: Mar 31, 2013
Posts: 111
    
    6

As the old saying goes there's more than one way to skin a cat..
 
It is sorta covered in the JavaRanch Style Guide.
 
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