Java Collection How to - Convert an Array to a List








Question

We would like to know how to convert an Array to a List.

Answer

    /*from w  w w  . j  a  va2s .  com*/

import java.util.Arrays;
import java.util.List;

public class Main {
    
    public static void main(String[] args) {
        String[] cars = {"A", "B", "C", "D"};
        List<String> carList = Arrays.asList(cars);
 
        for (String car : carList) {
            System.out.println(car);
        }
    }
}

The code above generates the following result.