New Array List, subList and Array. asList

Keywords: Java

List is a very common data structure in Java, so it is necessary for us to have a brief understanding of the three ways to create a List and to make some simple tests on them.

  • new ArrayList
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
System.out.println("----------new ArrayList----------");
for (Integer i : list) {
    System.out.println(i);
}
list.set(0, 10);
System.out.println("----------set modify----------");
for (Integer i : list) {
    System.out.println(i);
}
list.remove(0);
System.out.println("----------remove delete----------");
for (Integer i : list) {
    System.out.println(i);
}
list.add(0, 0);
System.out.println("----------add Add to----------");
for (Integer i : list) {
    System.out.println(i);
}
//Print results:
----------new ArrayList----------
1
2
3
4
5
----------setmodify----------
10
2
3
4
5
----------remove delete----------
2
3
4
5
----------addAdd to----------
0
2
3
4
5

The addition, deletion and modification of List created with new ArrayList <>() are normal.

  • subList
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
List<Integer> subList = list.subList(2, 4);
System.out.println("----------subList----------");
for (Integer i : subList) {
    System.out.println(i);
}
for (Integer i : list) {
    System.out.println("    " + i);
}
subList.set(0, 10);
System.out.println("----------set modify----------");
for (Integer i : subList) {
    System.out.println(i);
}
for (Integer i : list) {
    System.out.println("    " + i);
}
subList.remove(0);
System.out.println("----------remove delete----------");
for (Integer i : subList) {
    System.out.println(i);
}
for (Integer i : list) {
    System.out.println("    " + i);
}
subList.add(0, 0);
System.out.println("----------add Add to----------");
for (Integer i : subList) {
    System.out.println(i);
}
for (Integer i : list) {
    System.out.println("    " + i);
}
//Print results:
----------subList----------
3
4
    1
    2
    3
    4
    5
----------setmodify----------
10
4
    1
    2
    10
    4
    5
----------remove delete----------
4
    1
    2
    4
    5
----------addAdd to----------
0
4
    1
    2
    0
    4
    5

As shown above, the addition, deletion and modification of List are also normal. In fact, List created with subList returns SubList, the inner class of ArrayList, not ArrayList, but a view of ArrayList, and all operations on SubList sublists will eventually be reflected on the original list.

  • Arrays.asList
List<Integer> list = Arrays.asList(1,2,3,4,5);
System.out.println("----------Arrays.asList----------");
for (Integer i : list) {
    System.out.println(i);
}
list.set(0, 10);
System.out.println("----------set modify----------");
for (Integer i : list) {
    System.out.println(i);
}
list.remove(0);
System.out.println("----------remove delete----------");
for (Integer i : list) {
    System.out.println(i);
}
list.add(0, 0);
System.out.println("----------add Add to----------");
for (Integer i : list) {
    System.out.println(i);
}
//Print results:
----------Arrays.asList----------
1
2
3
4
5
----------setmodify----------
10
2
3
4
5
Exception in thread "main" java.lang.UnsupportedOperationException

As shown above, the set modification of List can be executed, but the remove and add methods throw an Unsupported OperationException exception error.
When using the tool class Arrays.asList() to convert an array into a collection, you cannot use its method to modify the collection-related, and its add/remove/clear method throws an Unsupported OperationException exception.
The return object of asList is an Array inner class, which does not implement the modification method of the collection. Arrays.asList embodies the adapter mode, but only the conversion interface, the background data is still an array.

summary

# add remove set clear
new ArrayList
subList
Arrays.asList

Posted by Sharkadder on Sun, 10 Feb 2019 14:33:17 -0800