[Java] Conversion from array to List

I want to convert from an array to a List

public static void main(String[] args) {
				
	String arr[] = {"orange","apple","cherry","melon","grape"};
	List<String> list = Arrays.asList(arr);

Assign Arrays.asList with arr as an argument to list

If you want to add the number of elements

NG example

	List<String> list = Arrays.asList(arr);
	list.add("banana");

(Cause) Arrays.asList () does not accept List method operations such as add and delete

OK example

	List<String> list = Arrays.asList(arr);
	List<String> list2 = new ArrayList<>(list);
	list2.add("banana");

Assign list to list2 as an argument of ArrayList If you write the above briefly

	List<String> list = new ArrayList<>(Arrays.asList(arr));
	list.add("banana");

Recommended Posts

[Java] Conversion from array to List
[Java] List type / Array type conversion
[Java] From two Lists to one array list
Java array / list / stream mutual conversion list
Type conversion from java BigDecimal type to String type
Changes from Java 8 to Java 11
Sum from Java_1 to 100
About Java Array List
Migration from Cobol to JAVA
New features from Java7 to Java8
Connect from Java to PostgreSQL
From Ineffective Java to Effective Java
How to initialize Java array
[Java] array
Java array
Java array
java (array)
Java array
[Java] Array
[Java] Convert 1-to-N List to Map
Java to be involved from today
Java array
From Java to VB.NET-Writing Contrast Memo-
[Java] How to use List [ArrayList]
For Java beginners: List, Map, Iterator / Array ... How to convert?
java array
[Java] Array
How to make a Java array
Java8 list conversion with Stream map
[Java] Convert array to ArrayList * Caution
[Java] Shallow copy and deep copy when converting an array to List
Cast an array of Strings to a List of Integers in Java
Effective Java Item 25 Select a list from an array First half
[Java] How to add data to List (add, addAll)
Convert from java UTC time to JST time
How to do base conversion in Java
Connect from Java to MySQL using Eclipse
Change List <Optional <T >> to Optional <List <T >> in Java
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
Immutable (immutable) List object conversion function in Java8
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Precautions when migrating from VB6.0 to JAVA
Java type conversion
java array variable
Java memorandum (list)
Clone Java List.
Array / list / map
[Java] Introduction to Java
About List [Java]
Run R from Java I want to run rJava
[Introduction to Java] About type conversion (cast, promotion)
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
How to get Class from Element in Java
[Java] How to switch from open jdk to oracle jdk
[Java] Get a random value from an array
Two-dimensional array by just starting to study Java
Minecraft BE server development from PHP to Java