[Java] Shallow copy and deep copy when converting an array to List

There are the following two methods to create a List object from an existing array.

  1. Use ```Arrays.asList (array) `` `
  2. Use the constructor of a List implementation class such as ArrayList

Arrays.asList(Array)If you use, it will be a shallow copy and you just pass the reference.


```arraylist```When using the constructor of list implementation class such as, it will be a new replication.


### Arrays.asList
 Just pass the reference as a shallow copy.

```java
        // Arrays.asList()Just pass a reference
        String[] ary = {"a", "b", "c"};
        List<String> list = Arrays.asList(ary);
        //  list.add("d"); // => java.lang.UnsupportedOperationException
        list.set(0, "e");
        System.out.println(Arrays.toString(ary)); //=> [e, b, c]The original layout has also changed.
        System.out.println(list);                 //=> [e, b, c]

It is possible to change the element with set, but be careful because an error will occur if you change the structure of an existing array such as add.

List implementation class constructor

It will be a new copy.


        //Create a new one by instantiating it with an implementation class of List such as ArrayList
        String[] ary2 = {"a", "b", "c"};
        List<String> list2 = new ArrayList<>(Arrays.asList(ary2));
        list2.add("d");
        list2.set(0, "e");
        System.out.println(Arrays.toString(ary2)); //=> [a, b, c]The original array is unchanged.
        System.out.println(list2);                 //=> [e, b, c, d]

Since new ArrayList <> (array) cannot be done, it is necessary to convert it to List object once.

Recommended Posts

[Java] Shallow copy and deep copy when converting an array to List
[Java] Precautions when converting variable-length argument parameters into an array
[Java] Declare and initialize an array
[Java] Conversion from array to List
Cast an array of Strings to a List of Integers in Java
[Java] Tips and error issues when converting from double to Big Decimal
[Java] From two Lists to one array list
Summary of good points and precautions when converting Java Android application to Kotlin
[Java] Program example to get the maximum and minimum values from an array
Summary when trying to use Solr in Java and getting an error (Solr 6.x)
When using a list in Java, java.awt.List comes out and an error occurs
About Java Array List
Null value is entered when assigning to an array
Gzip-compress byte array in Java and output to file
I want to make a list with kotlin and java!
ClassCastException occurs when migrating from Java7 to Java8 ~ Generics and overload ~
For Java beginners: List, Map, Iterator / Array ... How to convert?
[Java] List type / Array type conversion
[Java] Convert ArrayList to array
How to initialize Java array
Deep copy collection in Java
[Java] How to search for a value in an array (or list) with the contains method
Note No. 1 "Counting and displaying duplicate values in an array" [Java]
[Swift5] How to get an array and the complement of arrays
Sorting a list with an int array as an element (Java) (Comparator)
How to output the value when there is an array in the array
[Caution !!] Precautions when converting Rails devise and view files to haml
Effective Java Item 25 Select a list from an array First half
Resolved the error that occurred when trying to use Spark in an environment where Java 8 and Java 11 coexist.
Java learning memo (creating an array)
[Java] Convert 1-to-N List to Map
[Java] Difference between array and ArrayList
[Java] How to use List [ArrayList]
Surprisingly deep Java list inversion-Stream processing
How to make a Java array
Java array / list / stream mutual conversion list
[Java] Convert array to ArrayList * Caution
Update JAVA to the latest version to 1.8.0_144 (when downloading from the web and updating)
[Java] How to turn a two-dimensional array with an extended for statement
What to do and how to install when an error occurs in DXRuby 1.4.7
[Java small story] Monitor when a value is added to the List
[Introduction to Java] About array operations (1D array, 2D array declaration, instantiation, initialization and use)
I want to ForEach an array with a Lambda expression in Java
How to make an app with a plugin mechanism [C # and Java]