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

How to implement array conversion in Java collections

PHPz
PHPzforward
2023-04-22 19:37:061627browse

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()]);

2. Generate List

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

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);

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!

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