Home »
Java programming language
Java - Convert Array to ArrayList
By Preeti Jain Last updated : February 03, 2024
Problem statement
Given an array, you have to convert the given array to ArrayList using Java program.
Converting Array to ArrayList
To convert a given array to an ArrayList, you can use the Arrays.asList() method which is available in java.util package. This method accepts an array as its parameter and returns a list with array items (ArrayList).
Syntax
Below is the syntax to convert Array to ArrayList:
Java code to convert array to ArrayList
// Java program to demonstrate the example of
// conversion of an Array to an ArrayList with
// the help of asList() method of Arrays
import java.util.*;
public class ArrayToArrayList {
public static void main(String[] args) {
// array declaration
Integer arr[] = {
10,
20,
30,
40,
50
};
// Display array elements
System.out.println("Array elements");
for (int i = 0; i < arr.length; ++i)
System.out.println(arr[i]);
System.out.println();
// By using asList() method is used to convert
// Array to ArrayList
List arr_list = Arrays.asList(arr);
// Display ArrayList
System.out.println("ArrayList Elements:");
System.out.println(arr_list);
}
}
Output
The output of the above code is:
Array elements
10
20
30
40
50
ArrayList Elements:
[10, 20, 30, 40, 50]