Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Javarevisited
Blog about Java Program Tutorial Example How to, Unix Linux commands, Interview Questions, FIX Protocol, Tibco RV tutorials, Equities trading system, MySQL
Ads by Google Java Java Class Java Developer Java Tutorial
Its worth to note following point while using Arrays.asList()method for array to arraylist conversion:
to know more about Generic see my post How Generic works in Java
One of the most important point related to Arrays.asList() method is that it returns a fixed size List not a read +523
only List, although you can not add()or remove()elements on this List you can still change existing
elements by using set method. If you are using this to create read only List than its wrong, Use
Collections.unmodifiableList method to create read only collection in Java.
This example of converting an array to arraylist is not that efficient as the earlier method but its more
flexible.
javarevisited.blogspot.sg/2011/06/converting-array-to-arraylist-in-java.html 1/4
39/17/12
example of converting array to ArrayList and ArrayList to array in Java program
Javarevisited on Facebook
List assetList = new ArrayList();
String[] asset = {"equity", "stocks", "gold", Like
"foriegn exchange", "fixed income", "futures",
"options"}; 1,056 people like Javarevisited.
Collections.addAll(assetList, asset);
Important point about this method of array to ArrayList conversion F acebook social plugin
is :
1) Its not as fast as Arrays.asList() but more flexible.
2) This method actually copies the content of the underlying array into ArrayList provided.
3) Since you are creating copy of original array, you can add, modify and remove any element without affecting original
one.
4) If you wan to provide individual element you can do so by specifying them individually as comma separated. Which is a
very convenient way of inserting some more elements into existing list for example we can add some more asset classes
into our existing assetlist as below?
Subscribe
Arraylist newAssetList = new Arraylist(); By Javin Paul
newAssetList.addAll(Arrays.asList(asset));
Subscribe To This Blog Free
These were the three methods to convert an array to arraylist. You can also create arrays of ArrayList because list can
Posts
hold any type of object.
Comments
Tutorial
System.out.println("Size of List: " + currencyList.size());
System.out.println(currencyList);
Convert
API
► 2012 (139)
Now this is a opposite side of story where you want to convert from Arraylist to Array, instead of array to ArrayList
interesting right. Just now we were looking at converting array to arraylist and now we need to back to former one. Anyway ▼ 2011 (145)
it’s simpler than you have probably thought again we have java.util.Arraysclass as our rescue. This class acts as ► December (28)
bridge between ArrayList to array. ► November (14)
► October (14)
Example of converting ArrayList to Array in Java ► September (22)
In this example of arraylist to array we will convert our assetTradingList into String array.
► August (11)
► July (7)
ArrayList assetTradingList = new ArrayList(); ▼ June (9)
List of special bash parameter used in Unix or
assetTradingList.add("Stocks trading"); Li...
assetTradingList.add("futures and option trading"); 3 example of converting array to ArrayList and
assetTradingList.add("electronic trading"); Arr...
assetTradingList.add("forex trading"); 10 example of using Vim or VI editor in UNIX
assetTradingList.add("gold trading"); and L...
javarevisited.blogspot.sg/2011/06/converting-array-to-arraylist-in-java.html 2/4
39/17/12
example of converting array to ArrayList and ArrayList to array in Java program
assetTradingList.add("fixed income bond trading"); 3 ways to resolve NoClassDefFoundError in Java
String [] assetTradingArray = new String[assetTradingList.size()]; How to use Comparator and Comparable in
assetTradingArray.toArray(assetTradingArray); Java? With...
10 examples of grep command in UNIX and
After execution of last line our assetTradingList will be converted into String array. If you like to learn more about How Linux
to use ArrayList in Java efficiently check the link it has details on all popular operation on Java arraylist.
Top 30 Programming questions asked in
Interview - ...
Getting a clear idea of converting array to arraylist and back to arraylist and array saves a lot of time while writing code. It’s
Tibco tutorial : Reliability Parameter Explained
also useful if you like to initialize your arraylist with some default data or want to add some elements into your existing
arraylist. these examples of converting array to arraylist is by no means complete and please share your own ways of How Volatile in Java works ? Example of volatile
k...
converting from arraylist to array and vice-versa.
► May (6)
Related Java Tutorials
► April (10)
How to Solve Java.lang.OutOfMemoryError: Java Heap Space
How to Solve UnSupportedClassVersionError in Java ► March (4)
Example of Polymorphism in Java ► February (10)
What is abstraction in Java with Example
► January (10)
How to Split String in Java
How to Convert String to Date in Java ► 2010 (33)
How to Convert Integer to String in Java
Labels: core java, core java interview question, java collection tutorial
Tag Cloud
Anand said...
Nice One Javin!!!
Basic Details about the ArrayList in Java can be found by clicking here
Regards,
Anand.
GeekDude said...
Hi nice explanation..you can find the ArrayList to HashSet Conversion here....
ArrayList to HashSet
Anonymous said...
I agree for constructing arraylist from array in java, Arrays.asList() is best choice but constructed List here is
immutable and it doesn't allow add() or remove() elements from List. If you want to construct proper List from
javarevisited.blogspot.sg/2011/06/converting-array-to-arraylist-in-java.html 3/4
39/17/12
example of converting array to ArrayList and ArrayList to array in Java program
Arary in Java you need to rely on other examples like adding individual array element etc.
Post a Comment
Publish Preview
javarevisited.blogspot.sg/2011/06/converting-array-to-arraylist-in-java.html 4/4