SoFunction
Updated on 2025-04-10

Two simple implementation methods of array conversion list in Java

Method 1: Use the () method

()Methods can convert an array into a fixed-size List.

public static void main(String[] args) {
    int[] array = {a, b, c, d, e};
    List<Integer> list = (array);
    (list);  // Output: [a, b, c, d, e]}

Note: However, the size of this List is fixed, if you try to add or delete elements, otherwise the program will throw itUnsupportedOperationException. If you need a list that can be modified, you need to convert the returned list to a new ArrayList or other type of list.

Method 2: Use ArrayList constructor

public static void main(String[] args) {
    int[] array = {a, b, c, d, e};
    List<Integer> list = new ArrayList<>((array));
    (list);  // Output: [a, b, c, d, e]    ("f");  // Add elements, no exception is thrown    (list);  // Output: [a, b, c, d, e, f]}

use()Convert the array to a List and then use the ArrayList's constructor to create a new ArrayList. The new ArrayList can be modified, you can add or delete elements.

Attached list to array:

List<String> strList = new ArrayList<String>();
("aa");
("bb");
Object[] objs = ();

If you want to become a String array, you need to force the type to rotate.

String[] strs = (String[]) ();

You can also specify the size:

final int size = ();String[] strs = (String[])(new String[size]);

Summarize

This is the end of this article about two simple implementations of array conversion list in Java. For more related content on Java array conversion list, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!