Convert Java Array to List: Quick Guide

In Java, you can use the Arrays.asList() method to convert an array into a list. For example:

// 定义一个数组
String[] array = {"apple", "banana", "orange"};

// 将数组转换为列表
List<String> list = Arrays.asList(array);

// 打印列表
System.out.println(list);

The code above converts the array “array” into a list “list”, and the output is [apple, banana, orange]. It is important to note that the Arrays.asList() method returns a list of fixed size, meaning elements cannot be added or removed. If modification of the list is needed, you can use the ArrayList class to create a mutable list, as shown below:

// 使用ArrayList创建一个可修改的列表
List<String> mutableList = new ArrayList<>(Arrays.asList(array));

// 在列表末尾添加一个新元素
mutableList.add("grape");

// 打印修改后的列表
System.out.println(mutableList);

In the above code, a mutable list named mutableList is created, and a new element “grape” is added to the end of the list. The final output is [apple, banana, orange, grape].

bannerAds