[Java Tips] Add Array into a List and convert a List into an Array

Java array to list to array conversions with inbuilt features.

With Java, putting contents of an Array into a new List object or adding into an existing List object can be achieved easily using a for() loop; just by going through each and every element in array and adding to List one at a time. But that is not needed. Java has provided a method to achieve that easily; just with one method call. Following code snippet shows how.
import java.util.List;
import java.util.Arrays;

String[] array = {"one", "two", "three"};
List newListObject = Arrays.asList(array);

//adding to existing List
String[] newArray = {"four", "five"};

List all = new ArrayList();
all.addAll(newListObject);
all.addAll(Arrays.asList(newArray));

Also creating a new Array object using an existing List object can be done using another for() loop; by creating a new Array object with a size matching to list size and adding each on at a time. But for this requirement, there are a set of methods.
List list = new ArrayList();
list.add("one");
list.add("two");

Object[] array1 = list.toArray(); //1
String[] array2 = (String[])list.toArray(new String[0]); //2

String[] array3 = new String[2];
list.toArray(array3); //3

With Line #1, returned array is of Object type, while #2 returns a new array object of String type.
Line #3 uses the same method used in #2, but in this case we have provided an array object with the same size as the list. Because of that in line #3, the provided array object is populated with list elements.

COMMENTS

BLOGGER: 10
Loading...

Read More...

Name

About,2,Adsense,3,Ant,1,Apache,3,Axis,3,Blogger,1,Books,1,CentOS,2,Chrome,2,CSS,2,Database,3,Earn Online,3,Eclipse,10,Facebook,1,Firefox,10,Gmail,4,GNU/Linux,9,Google,26,GWT,8,Hardware,2,IE,5,Interesting,15,Internet,14,Java,49,Javascript,7,JBoss,1,Jenkins,1,Log4j,2,Me,6,Microsoft,2,Miscellaneous,1,News,11,Opinion,10,OSGi,1,PHP,1,Productivity,3,Programming,36,Puzzle,3,Security,4,Software,41,Sports,9,Spring,2,Story,6,Subversion,3,TDD,4,Tech,2,Tips,1,Tomcat,6,Tutorial,13,Ubuntu,4,Web application,14,Web Design,2,Web services,3,Windows,10,Yahoo,1,Zip,2,
ltr
item
Digizol: [Java Tips] Add Array into a List and convert a List into an Array
[Java Tips] Add Array into a List and convert a List into an Array
Java array to list to array conversions with inbuilt features.
https://1.bp.blogspot.com/-hAWTnL99ehk/Wejm7VsWeWI/AAAAAAAACZg/OeW_DsKmubIccfq-TNDhsd87naatEqchwCLcBGAs/s1600/Java%2Blist%2Bto%2Barray%2Bto%2Blist%2Bwww.digizol.com.png
https://1.bp.blogspot.com/-hAWTnL99ehk/Wejm7VsWeWI/AAAAAAAACZg/OeW_DsKmubIccfq-TNDhsd87naatEqchwCLcBGAs/s72-c/Java%2Blist%2Bto%2Barray%2Bto%2Blist%2Bwww.digizol.com.png
Digizol
https://www.digizol.com/2007/09/java-array-list-array-convert-insert.html
https://www.digizol.com/
https://www.digizol.com/
https://www.digizol.com/2007/09/java-array-list-array-convert-insert.html
true
7440473
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy