Java Arrays. asList usage, javaarrays. aslist
- Import java. util. Arrays;
- Import java. util. List;
- /**
- *
- * This class demonstrates the asList method in the Arrays class.
- * Four paragraphs are used to demonstrate the features of the method.
- *
- * (1) this method does not support arrays of the basic data type. It is not recommended when arrays are of the basic data type.
- * (2) when the asList () method is used, the array is linked with the list.
- * When one of the updates is made, the other will automatically get the updates.
- * Note: The basic data type array does not have this attribute only for the object array type.
- * (3) the array obtained by asList does not have the add or remove methods.
- *
- * Reading: You can check the source code of the Arrays class. The List returned by asList is implemented in Array.
- * Internal class, but this class does not define the add and remove methods. In addition, why modify one of them, the other is automatically
- * Updated, because the asList obtains the List and actually references the array.
- */
- Public class AsListTest {
- Public static void main (String [] args ){
- /* Section 1: problems in using asList for basic data types */
- /* Note: Although JDK1.6 can convert an array of the basic data type to a List, it still has a defect */
- Int [] a_int = {1, 2, 3, 4 };
- /* The expected output should be 1, 2, 3, 4, but the output is actually just a reference. Here it treats a_int as an element */
- List a_int_List = Arrays. asList (a_int );
- Foreach (a_int_List );
- /* For this reason, we need to traverse the elements in this way */
- ForeachForBase (a_int_List );
- /* Section 2: Use asList for an array of object types as expected */
- Integer [] a_Integer = new Integer [] {1, 2, 3, 4 };
- List a_Integer_List = Arrays. asList (a_Integer );
- Foreach (a_Integer_List );
- /* Section 3: When the List after the array or asList is updated, the other will automatically get the update */
- A_Integer_List.set (0, 0 );
- Foreach (a_Integer_List );
- Foreach (a_Integer );
- A_Integer [0] = 5;
- Foreach (a_Integer_List );
- Foreach (a_Integer );
- /* Section 4: For an array of basic types, after modifying the corresponding value in the List after asList, an exception is reported during running.
- * However, the List corresponding to the basic type array will change, which is undoubtedly
- */
- /*
- * A_int_List.set (0, 0 );
- * Foreach (a_int_List); foreach (a_int );
- */
- A_int [0] = 5;
- ForeachForBase (a_int_List );
- Foreach (a_int );
- }
- /* Print 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 ();
- }
- }