Home > Java > javaTutorial > How to implement array conversion in Java collections

How to implement array conversion in Java collections

PHPz
Release: 2023-04-22 19:37:06
forward
1623 people have browsed it

1. toArray() method

You can use the toArray() method in List to convert the List into an array. Note: If there are no parameters in the toArray() method, it will be returned. It is an object type array. If it is of string type, you need to add: new String[list.size()];

List<String> list=new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
String[] array=list.toArray(new String[list.size()]);
Copy after login

2. Generate List

String[] array=new String[]{"aa","bb","cc"};
List<list> list=Arrays.asList(array);
Copy after login

Note : The List generated by the above method cannot perform addition or deletion operations. The following operations have no such restrictions.

String[] array=new String[]{"aa","bb","cc"};
List<String> list=new ArrayList<String>();
Collections.addAll(list,array);
Copy after login

What collection classes are there in Java?

Collections in Java are mainly divided into Four categories:

1. List: ordered and repeatable;

2. Queue: ordered and repeatable;

3. Set : Non-repeatable;

4. Map mapping: unordered, unique keys, not unique values.

The above is the detailed content of How to implement array conversion in Java collections. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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