Home > Java > javaTutorial > body text

Convert array to List using java's Arrays.asList() function

WBOY
Release: 2023-07-25 08:01:06
Original
952 people have browsed it

Use java's Arrays.asList() function to convert an array into a List

In Java programming, you often encounter the situation of converting an array into a List. To achieve this conversion, you can use the asList() function in the java.util.Arrays class. This function can convert an array into a List to facilitate the operation and processing of the array.

The following is a sample code to convert an array to a List using the Arrays.asList() function:

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

public class ArrayToListExample {

    public static void main(String[] args) {
        String[] array = {"apple", "banana", "orange"};
        
        // 将数组转换为List
        List<String> list = Arrays.asList(array);
        
        // 打印List元素
        System.out.println(list);
    }
    
}
Copy after login

In the above example, we have an array of type Stringarray, which contains the names of three fruits. Convert the array to a List by calling Arrays.asList(array), and then assign the returned List to the list variable. Finally, we print out the elements of list.

Run the above code, the following results will be output:

[apple, banana, orange]
Copy after login

By using the Arrays.asList() function, we convert the array into a List, and can operate the array like a List. For example, we can access array elements through subscripts, and we can also use List methods to add, delete, modify, and other operations on the array.

It should be noted that the Arrays.asList() function returns a fixed-size List, that is, its size cannot be changed. Therefore, if we add or delete elements to the returned List, an UnsupportedOperationException will be thrown. If you need to modify the List, you can first convert it into a variable ArrayList, as shown below:

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

public class ArrayToListExample {

    public static void main(String[] args) {
        String[] array = {"apple", "banana", "orange"};
        
        // 将数组转换为List
        List<String> list = new ArrayList<>(Arrays.asList(array));
        
        // 添加新元素
        list.add("grape");
        
        // 打印List元素
        System.out.println(list);
    }
    
}
Copy after login

In the above code, we use the constructor of ArrayList to return the value of Arrays.asList() Values ​​are converted to a mutable ArrayList. In this way, we can add new elements to the List.

Through the above examples and explanations, we can see that using java's Arrays.asList() function can easily convert an array into a List in order to process and operate the array. Whether it is traversing and accessing array elements, or performing operations such as adding, deleting, or modifying arrays, the converted List can easily meet our needs. Therefore, during development, we can flexibly use the Arrays.asList() function to improve the readability and simplicity of the code.

The above is the detailed content of Convert array to List using java's Arrays.asList() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!