Convert an Array to ArrayList in Java


Write a Java Program to convert an array to arraylist.

Click Here to View the Solution!
import java.util.Arrays;
import java.util.ArrayList;
class ArraytoArraylist {
  public static void main(String[] args) {
    // create an array
    String[] array = {"New York", "London", "Washington"};
    System.out.println("Array: " + Arrays.toString(array));
    // convert array to arraylist
    ArrayList<String> languages= new ArrayList<>(Arrays.asList(array));
     System.out.println("ArrayList: " + languages);
Click Here to View the Output!
 Array: [New York, London, Washington]
 ArrayList: [New York, London, Washington]
Click Here to View the Explanation!
  • In this program, first a String type array has been created which is also named as array.
  • For conversion of the array into an arraylist, the following code has been utilized: Arrays.asList(array)
  • Here array has been passed as an argument to the Array method asList() which converts array into an arraylist.
%d bloggers like this: