Today, when you add a single ArrayList object to another ArrayList, it is always the same memory address as the source list and the destination list. The reasons are as follows:
accidentally saw the collections copy (List Desc,list src) method. Just wanted this method and initialize a list desc = new ArrayList (list C) " The parameter must implement the collection interface "difference.
The difference between the two is very large, the latter is a shallow copy, just copy the elements of the source list, copy only reference. The elements (references) of the two lists are different, but the reference points to the same object. That is, each element of the two list points to or through one memory. However, the former is a deep copy, not only copy the SRC Element (reference), src within each element of the object is a copy. That is, each element of the two list points to not the same memory.
So the Collections.copy () method is used to copy, but it is exposed to the exception reported by this method:
The result of using the latter to copy is that when your desc list changes, SRC will change as well.
When using the former for copying you must also be aware that the target list length must be greater or equal than the length of the source linked list.
Examples are as follows:
List Src1=NewArrayList (3)
Src1.add ("A");
Src2.add ("B");
Src3.add ("C");
If you use the following method to copy a linked list
/*******************************/
List des1=NewArrayList (3);
Collections.copy (DES1,SRC1);
/*******************************/
An error will be thrown, throwing an array out of bounds exception.
At that time I think all do not understand why, clearly already set the length of 3, why still error!
Later printed out des1.size () to know that the length of the des1 is 0;3 that the list has a capacity of 3, not to say that there are 3 elements in des1. The view API does not know that its capacity (capacity size) can be specified (preferably specified). When initialized, size is always defaulted to 0, and size changes only when related operations such as add and remove are performed. for copy (), however, the first thing to do is to compare the size of the DESC1 with the sizes of the SRC1, and only copy if DESC1 size is greater than or equal to SRC1 size. Otherwise throws a indexoutofboundsexception exception.
So you can specify the size of the target desc by the following method
/****************************** */
List des1= new arraylist (Array.aslist (new object[src1.size])); // note: new arraylist (collection col) The parameter must implement the collection interface.
collections.copy (DES1,SRC1);
/****************************** */
after executing the first sentence, size is 3, but it is a shallow copy of an empty array.
Using this guy's method, my side has always reported that this constructor could not be found because I used the jdk6 reason, and later used the alternative method, with Apache Collectionutils:
dest1 = new ArrayList ();
Collectionutils.addall (Dest1, new object[src1.size ()]);
Collections.copy (Dest1, SRC1);
Java.util.Collections.copy () method Note point