[JAVA] Trap of asList of Arrays class

Introduction

I've been using Arrays a lot lately, and I've been addicted to it. I will write it as a memo.

To make an array

When using arrays in Java, there are the following two methods. How to generate using new and how to generate using asList of Arrays class.

Both can create a string array with "test1" and "test2".

Try to manipulate the array

Now that we have created the array, let's add / edit / delete it. The test code looks like this

sample.java: Case using Arrays


@Test
public void test_sample() {

    List<String> test = Arrays.<String>asList("aaa", "bbb");

    // 1.Add element
    test.add("ccc");

    // 2.Delete element
    test.remove(1);

    // 3.Edit element
    test.set(1, "ddd");

    // 4.Clear all elements
    test.clear();
}

When I try to do this, except in case 3 java.lang.UnsupportedOperationException will occur.

But what about the case with new?

sample2.java: case using new


@Test
public void test_sample2() {

    List<String> test = new ArrayList<>();
    test.add("aaa");
    test.add("bbb");

    // 1.Add element
    test.add("ccc");

    // 2.Delete element
    test.remove(1);

    // 3.Edit element
    test.set(1, "ddd");

    // 4.Clear all elements
    test.clear();
}

In this case, no error will occur even if you execute it, and the result will be as follows.

Contents of the array after the case of 1: aaa, bbb, ccc Contents of the array after the second case: aaa, ccc Contents of the array after case 3: aaa, ddd Contents of the array after the case of 4: Empty

why?

Why is there a difference when both are just doing the same array operation? Using new will dynamically generate an array, but for asList in the Arrays class

This is because it returns a list of fixed sizes **.

It was also mentioned in the Java API specification. Excerpt from Class Arrays

Qualifiers and types Method description
static <T> List<T> asList(T... a)
Link to the specified array
Returns a list of fixed sizes

What is a fixed size list?

When using new, the object of the specified element Generate. Therefore ** an entity exists **.

But in the case of Arrays.asList, I specified Like a list holding the contents of an element ** Just pretending to be **, so fixed size (fixed number) It becomes a list of If you do any operations related to numbers, you will get an UnsupportedOperationException error as described above.

Summary

The list generated by Arrays.asList controls the number of elements I couldn't do it, so I used a dynamic list Not suitable for processing (though I don't think it is normally used)

However, when I want to handle it as a list type with a fixed number of elements (which cannot be changed), I personally think that it is better because it does not allocate inadvertent memory than adding it to the object created by new.

Recommended Posts

Trap of asList of Arrays class
Use of Date class
Basic methods of Ruby arrays
[GCD] Basics of DispatchQueue class
Example of using abstract class
[Java] Comparator of Collection class
Summary of Java Math class
Various methods of Java String class
Various methods of the String class
[Spring Boot] Role of each class