Arraylist is one of the commonly used sets. I don't know if you are a javaner or have no time to look at the source code of arraylist. If you read it, you will think that arraylist is actually one thing, right? Let's take a look at some source code of arraylist.
1 public class arraylist <E> extends actlist <E> 2 implements list <E>, randomaccess, cloneable, Java. io. serializable 3 {4 Private Static final long serialversionuid = 8683452584252892189l; 5 6 // set the default arraylist capacity to 7 Private Static final int default_capacity = 10; 8 9 // empty array, when a non-parameter constructor is called, an empty array 10 Private Static final object [] empty_elementdata ={}; 11 12 // This is the array 13 private transient object that truly saves data. [] Elementdata; 14 15 // number of actual arraylist elements 16 private int size; 17 18 // constructor input default Capacity Set Default array size 19 public arraylist (INT initialcapacity) {20 super (); 21 if (initialcapacity <0) 22 throw new illegalargumentexception ("illegal capacity:" + 23 initialcapacity); 24 this. elementdata = new object [initialcapacity]; 25} 26 27 // The default non-parameter constructor is an empty array 28 public arraylist () {29 super (); 30 this. elementdata = EMP Ty_elementdata; 31} 32 33 // The constructor transmits the values in the collection to arraylist 34 public arraylist (collection <? Extends E> C) {35 elementdata = C. toarray (); 36 size = elementdata. length; 37 // C. toarray might (incorrectly) not return object [] (see 6260652) 38 If (elementdata. getclass ()! = Object []. class) 39 elementdata = arrays. copyof (elementdata, size, object []. class); 40} 41 42 // The following describes how arraylist dynamically expands the array to implement add and remove 43 44 45 public Boolean add (E) {46 ensurecapacityinternal (size + 1); // increments modcount !! 47 elementdata [size ++] = E; 48 return true; 49} 50 51 52 public void add (INT index, e element) {53 rangecheckforadd (INDEX ); 54 55 ensurecapacityinternal (size + 1); // increments modcount !! 56 system. arraycopy (elementdata, index, elementdata, index + 1, 57 size-index); 58 elementdata [Index] = element; 59 size ++; 60} 61 62 private void ensurecapacityinternal (INT mincapacity) {63 If (elementdata = empty_elementdata) {64 mincapacity = math. max (default_capacity, mincapacity); 65} 66 67 ensureexplicitcapacity (mincapacity); 68} 69 70 Private void ensureexplicitcapacity (INT mincapac Ity) {71 modcount ++; 72 73 // exceeds the length of the array, and 74 if (mincapacity-elementdata need to be dynamically expanded. length> 0) 75 grow (mincapacity); 76} 77 78 // This is the essence of dynamic expansion. Seeing this method, arraylist is instantly played back to its original shape 79 private void grow (INT mincapacity) {80 int oldcapacity = elementdata. length; 81 // set the capacity of the new array to 1.5 times that of the original array. 82 int newcapacity = oldcapacity + (oldcapacity> 1 ); 83 // check whether the capacity of the new array is sufficient. If it is enough, use this length to create a new array. If 84 // is not enough, set the length of the array to 85 if (newca Pacity-mincapacity <0) 86 newcapacity = mincapacity; 87 // determines if the maximum value is 88 If (newcapacity-max_array_size> 0) 89 newcapacity = hugecapacity (mincapacity ); 90 // copy the original array value to the new array, and the reference of arraylist points to the new array 91 // an array is created here. If the data volume is large, the array is created repeatedly, efficiency is still affected. 92 // we recommend that you specify the default capaticy size 93 elementdata = arrays by using the constructor when appropriate. copyof (elementdata, newcapacity); 94} 95 96 Private Static int hugecapacity (INT mincapacity) {97 If (mincapacity <0) // overflow 98 throw new outofmemoryerror (); 99 return (mincapacity> max_array_size )? 100 integer. max_value: 101 max_array_size; 102} 103 104 105}
In fact, the essence of arraylist is array, and arraylist is to dynamically expand the array. Its operations such as ADD, get, and remove are the operations on the array. So far, do you still need to remember the features of the arraylist described above? If you want to remember it, it will be weak! Some features of arraylist come from Arrays: ordered, repeatable elements, slow insertion, fast indexing, and so on. Do you understand? Suddenly enlightened
Source code analysis of arraylist