2525
2626package java .util ;
2727
28+ import sun .misc .SharedSecrets ;
29+
2830import java .util .function .Consumer ;
2931import java .util .function .Predicate ;
3032import java .util .function .UnaryOperator ;
31- import sun .misc .SharedSecrets ;
3233
3334/**
3435 * Resizable-array implementation of the <tt>List</tt> interface. Implements
@@ -110,12 +111,13 @@ public class ArrayList<E> extends AbstractList<E>
110111 private static final long serialVersionUID = 8683452581122892189L ;
111112
112113 /**
113- * Default initial capacity.
114+ * Default initial capacity. 默认容量
114115 */
115116 private static final int DEFAULT_CAPACITY = 10 ;
116117
117118 /**
118119 * Shared empty array instance used for empty instances.
120+ * size=0的空实例对象
119121 */
120122 private static final Object [] EMPTY_ELEMENTDATA = {};
121123
@@ -131,19 +133,20 @@ public class ArrayList<E> extends AbstractList<E>
131133 * The capacity of the ArrayList is the length of this array buffer. Any
132134 * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
133135 * will be expanded to DEFAULT_CAPACITY when the first element is added.
136+ * 保存ArrayList中数据的数组
134137 */
135138 transient Object [] elementData ; // non-private to simplify nested class access
136139
137140 /**
138141 * The size of the ArrayList (the number of elements it contains).
139- *
142+ * ArrayList的实际数据的数量
140143 * @serial
141144 */
142145 private int size ;
143146
144147 /**
145148 * Constructs an empty list with the specified initial capacity.
146- *
149+ * 带容量参数的构造
147150 * @param initialCapacity the initial capacity of the list
148151 * @throws IllegalArgumentException if the specified initial capacity
149152 * is negative
@@ -152,6 +155,7 @@ public ArrayList(int initialCapacity) {
152155 if (initialCapacity > 0 ) {
153156 this .elementData = new Object [initialCapacity ];
154157 } else if (initialCapacity == 0 ) {
158+ // 1.6版本的是直接初始化一个容量为10的对象,现在是初始化为容量=0的对象
155159 this .elementData = EMPTY_ELEMENTDATA ;
156160 } else {
157161 throw new IllegalArgumentException ("Illegal Capacity: " +
@@ -161,6 +165,7 @@ public ArrayList(int initialCapacity) {
161165
162166 /**
163167 * Constructs an empty list with an initial capacity of ten.
168+ * 空参构造,默认容量=0,1.6版本的默认容量为10
164169 */
165170 public ArrayList () {
166171 this .elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA ;
@@ -170,7 +175,7 @@ public ArrayList() {
170175 * Constructs a list containing the elements of the specified
171176 * collection, in the order they are returned by the collection's
172177 * iterator.
173- *
178+ * 创建一个包含collection的ArrayList
174179 * @param c the collection whose elements are to be placed into this list
175180 * @throws NullPointerException if the specified collection is null
176181 */
@@ -181,7 +186,7 @@ public ArrayList(Collection<? extends E> c) {
181186 if (elementData .getClass () != Object [].class )
182187 elementData = Arrays .copyOf (elementData , size , Object [].class );
183188 } else {
184- // replace with empty array.
189+ // replace with empty array. 依然将数据为0的情况独立出来,替换为容量=0的对象
185190 this .elementData = EMPTY_ELEMENTDATA ;
186191 }
187192 }
@@ -190,6 +195,7 @@ public ArrayList(Collection<? extends E> c) {
190195 * Trims the capacity of this <tt>ArrayList</tt> instance to be the
191196 * list's current size. An application can use this operation to minimize
192197 * the storage of an <tt>ArrayList</tt> instance.
198+ * 将当前容量值设为 =实际元素个数
193199 */
194200 public void trimToSize () {
195201 modCount ++;
@@ -204,6 +210,7 @@ public void trimToSize() {
204210 * Increases the capacity of this <tt>ArrayList</tt> instance, if
205211 * necessary, to ensure that it can hold at least the number of elements
206212 * specified by the minimum capacity argument.
213+ * 确定ArrayList的容量
207214 *
208215 * @param minCapacity the desired minimum capacity
209216 */
0 commit comments