Convert Array to ArrayList in Java

In the earlier tutorial, we have seen how to convert ArrayList to Array in Java. Sometimes, we may need to convert Array to Arraylist in Java. There are 2 ways to convert Array to ArrayList in Java. Let’s see these 2 methods one by one.

Using Arrays.asList():

Arrays class provides a method asList() to convert Array into ArrayList in Java. This is the simplest way to convert Array to ArrayList in java. The syntax of the asList() method is as follow:

Syntax:

asList() method returns a fixed-size list backed by the specified array. The returned list is serializable and implements RandomAccess. This method acts as bridge between array-based and collection-based APIs, in combination with Collection.toArray().

Example:

Output:

Using Collections.addAll():

Collections class provides addAll() method to convert Array to ArrayList in Java. The syntax of the addAll() method is as follows:

Syntax:

where, c ia the collection into which elements are to be inserted.
elements – the elements to insert into collection c

The addAll() method adds all the specified elements to the specified collection. Elements to be added may be specified individually or as an array.

Example:

Output:

In this tutorial, we have seen the 2 ways of converting an Array into ArrayList.

Related posts

Leave a Comment