When I used Java for a long time, I got stuck in Arrays.asList () a little, so I will write down the notes of Arrays.asList () as a memorandum.
String[] words = {"abc","def","ghi"};
List<String> list = Arrays.asList(words);
Returns a fixed size list that works with the specified array.
In [Java API](https://docs.oracle.com/javase/jp/8/docs/api/java/util/Arrays.html#asList-T ...-), write as above. It has been done. This means that the length of the list is fixed, even if the return value of the asList method is a list.
For example, if you try to change the number of elements with the add method or remove method for the return value of the asList method, a compile error will not occur, but at runtime [java.lang.UnsupportedOperationException](https://docs.oracle. com / javase / jp / 8 / docs / api / java / lang / UnsupportedOperationException.html) will occur.
This point is also written in the API, so if you check the API properly, you can clear it without any problems.
//Adding elements to a fixed length list
static void add() {
List<Integer> fixedSizeList = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
//UnsupportedOperationException occurs at runtime.
fixedSizeList.add(11);
}
//Deleting elements for fixed-length lists
static void remove() {
List<Integer> fixedSizeList = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
//UnsupportedOperationException occurs at runtime.
fixedSizeList.remove(5);
}
The problem of "list length is fixed" can be avoided by "specify the list generated by the asList method in the constructor of ArrayList" as shown below.
Conversion from list to array can be done in one shot by using List # toArray (), but conversion from array to variable length list seems to be inevitably two steps.
// Arrays.asList()Convert the list created in step 3 to a variable length (mutable) list.
static void add2() {
List<Integer> fixedSizeList = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
List<Integer> list = new ArrayList<>(fixedSizeList);
//No error at run time.
list.add(11);
}
If you specify an array of primitive type in the argument of Arrays.asList (), the return value will not be List <wrapper class type>
.
For example, if you specify int [] as an argument of Arrays.asList (), the return value will be List <int []>
.
This wasn't written in the API at all, so I was worried that it wouldn't compile at first, but as a result, I was relieved to find that it worked as specified.
It seems that there is a method such as "specify an array of wrapper class type as an argument" to avoid this problem, but it seems strange to bother to convert it to a wrapper class type.
static void create1() {
//If it is a reference type array, the reference type List will be returned as expected....
String[] words = {"abc","def","ghi"};
List<String> list1 = Arrays.asList(words);
}
static void create2() {
//AsList an array of primitive types()If it is used as an argument of, a list with 1 element is returned because "the array becomes one element in the list".
// list2.size()Is 1.
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
List<int[]> list2 = Arrays.asList(numbers);
System.out.println("length of list2:" + list2.size());
}
static void create3() {
//Variadic argument asList()If you pass it to, the list will be generated as expected even if the argument is an integer value (= int).
// list3.size()Is 10.
List<Integer> list3 = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
System.out.println("length of list3:" + list3.size());
}
Recommended Posts