Home >Java >javaTutorial >How to convert Arrays to list in java

How to convert Arrays to list in java

WBOY
WBOYforward
2023-05-03 10:22:061098browse

1. Description

(1) List is only used for traversal, so use Arrays.asList().

(2) If you want to add or delete elements from List, just create a new java.util.ArrayList, and then add elements one by one.

2. Points to note

(1) This method is suitable for arrays of object type data (String, Integer...)

(2) This method The method is not recommended for use with arrays of basic data types (byte, short, int, long, float, double, boolean)

(3) This method links the array with the List list: when one of them is updated, Another automatic update

(4) does not support add(), remove(), clear() and other methods

3. The instance

will The "ArrayList" constructed by Arrays.asList() is passed into the construction method of java.util.ArrayList.

Integer[] a = new Integer[]{1, 2, 3};
List list = Arrays.asList(a);
ArrayList arrayList = new ArrayList<>(list);

This constructor uses the Arrays.copyOf method, so the array inside java.util.ArrayList will not have any relationship with the incoming array.

What are the basic data types of java

The basic data types of Java are divided into:

1. Integer type, used to represent the data type of integer.

2. Floating point type, a data type used to represent decimals.

3. Character type. The keyword of character type is "char".

4. Boolean type is the basic data type that represents logical values.

The above is the detailed content of How to convert Arrays to list in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete