Java array to arraylist conversion

HQ ┬╗ Java Tutorial ┬╗ Example Source Code ┬╗ Java Array Examples ┬╗ Java array to arraylist conversion
Java array to arraylist conversion 2018-02-16T07:27:54+00:00

On this section we will be discussing regarding java array to arraylist conversion. At the end of this tutorial we would shed light on how to handle the conversion of array to arraylist and how to print the contents of an arraylist by using advance for loop (for each). The conversion is easy we just have to call the static asLList method of java.util.Arrays class.

Supposed we have an array of Strings, convert it into List. After conversion verify the elements the array contains by printing it through the console.

Java array to arraylist conversion – Using the asList method

Sample Output:

As you can see from the source code provided, the asList method makes our life easier.

Java array to arraylist conversion – Using for loop

Above method is the best solution however if you prefer a long method, we can always iterate through the elements of your array and then assign it one by one to the List by calling the add method.

 

Sample Output

Running above example will give the following output

Java array to arraylist conversion example

Java array to arraylist conversion example

[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close