Arrays.asList method summary
- 2020-06-01 09:53:59
- OfStack
Without further ado, look at the code:
import java.util.Arrays;
import java.util.List;
/**
*
* This class demonstrates Arrays In the class asList methods
* through 4 Let's do it in paragraphs , The characteristics of this method are shown .
*
* (1) This method does not support arrays of basic data types well , It is not recommended when an array is a primitive data type
* (2) When using asList() Method, the array is linked to the list 1 the .
* When updating one of them 1 , the other 1 Four will be updated automatically.
* Pay attention to : Only for object array types , Arrays of basic data types do not have this feature
* (3) asList The array you get is none add and remove methods
*
* Read the relevant : By looking at the Arrays The source of the class can be known ,asList The returned List is Array Implemented in
* The inner class , This class is not defined add and remove methods . In addition , Why change it 1 a , On the other 1 An automatic also
* It's updated , because asList To obtain List The actual reference is to the array
*/
public class AsListTest {
public static void main(String[] args) {
/* The paragraph 1: Basic data type usage asList The problems in the */
/* instructions : Although the JDK1.6 Is able to convert an array of basic data types into List, But there is a catch */
int[] a_int = { 1, 2, 3, 4 };
/* The expected output should be 1,2,3,4, But the actual output is just 1 A reference , Here it a_int As a 1 An element */
List a_int_List = Arrays.asList(a_int);
foreach(a_int_List);
/* To do that we need to go through the elements like this */
foreachForBase(a_int_List);
/* The paragraph 2: Use of an array of object types asList, That's what we expected */
Integer[] a_Integer = new Integer[] { 1, 2, 3, 4 };
List a_Integer_List = Arrays.asList(a_Integer);
foreach(a_Integer_List);
/* The paragraph 3: When updating an array or asList After the List, On the other 1 Four will be updated automatically */
a_Integer_List.set(0, 0);
foreach(a_Integer_List);
foreach(a_Integer);
a_Integer[0] = 5;
foreach(a_Integer_List);
foreach(a_Integer);
/* The paragraph 4: Array of primitive types , through asList After the List After the corresponding value is modified , An exception is reported at run time
* But the array of primitive types corresponds to List It's going to change , There is no doubt about it
*/
/*
* a_int_List.set(0, 0);
* foreach(a_int_List); foreach(a_int);
*/
a_int[0] = 5;
foreachForBase(a_int_List);
foreach(a_int);
}
/* Printing method */
private static void foreach(List list) {
for (Object object : list) {
System.out.print(object + " ");
}
System.out.println();
}
private static void foreachForBase(List a_int_List) {
int[] _a_int = (int[]) a_int_List.get(0);
foreach(_a_int);
}
private static void foreach(int[] a_int) {
for (int i : a_int) {
System.out.print(i + " ");
}
System.out.println();
}
private static void foreach(Integer[] _a_Integer) {
for (int i : _a_Integer) {
System.out.print(i + " ");
}
System.out.println();
}
}