📄 arraylist.java
字号:
else if (a.length > size) a[size] = null; System.arraycopy(data, 0, a, 0, size); return a; } /** * Retrieves the element at the user-supplied index. * * @param index the index of the element we are fetching * @throws IndexOutOfBoundsException if index < 0 || index >= size() */ public Object get(int index) { checkBoundExclusive(index); return data[index]; } /** * Sets the element at the specified index. * * @param index the index at which the element is being set * @param e the element to be set * @return the element previously at the specified index * @throws IndexOutOfBoundsException if index < 0 || index >= 0 */ public Object set(int index, Object e) { checkBoundExclusive(index); Object result = data[index]; data[index] = e; return result; } /** * Appends the supplied element to the end of this list. * * @param e the element to be appended to this list * @return true, the add will always succeed */ public boolean add(Object e) { modCount++; if (size == data.length) ensureCapacity(size + 1); data[size++] = e; return true; } /** * Adds the supplied element at the specified index, shifting all * elements currently at that index or higher one to the right. * * @param index the index at which the element is being added * @param e the item being added * @throws IndexOutOfBoundsException if index < 0 || index > size() */ public void add(int index, Object e) { checkBoundInclusive(index); modCount++; if (size == data.length) ensureCapacity(size + 1); if (index != size) System.arraycopy(data, index, data, index + 1, size - index); data[index] = e; size++; } /** * Removes the element at the user-supplied index. * * @param index the index of the element to be removed * @return the removed Object * @throws IndexOutOfBoundsException if index < 0 || index >= size() */ public Object remove(int index) { checkBoundExclusive(index); Object r = data[index]; modCount++; if (index != --size) System.arraycopy(data, index + 1, data, index, size - index); // Aid for garbage collection by releasing this pointer. data[size] = null; return r; } /** * Removes all elements from this List */ public void clear() { if (size > 0) { modCount++; // Allow for garbage collection. Arrays.fill(data, 0, size, null); size = 0; } } /** * Add each element in the supplied Collection to this List. It is undefined * what happens if you modify the list while this is taking place; for * example, if the collection contains this list. * * @param c a Collection containing elements to be added to this List * @return true if the list was modified, in other words c is not empty * @throws NullPointerException if c is null */ public boolean addAll(Collection c) { return addAll(size, c); } /** * Add all elements in the supplied collection, inserting them beginning * at the specified index. * * @param index the index at which the elements will be inserted * @param c the Collection containing the elements to be inserted * @throws IndexOutOfBoundsException if index < 0 || index > 0 * @throws NullPointerException if c is null */ public boolean addAll(int index, Collection c) { checkBoundInclusive(index); Iterator itr = c.iterator(); int csize = c.size(); modCount++; if (csize + size > data.length) ensureCapacity(size + csize); int end = index + csize; if (size > 0 && index != size) System.arraycopy(data, index, data, end, size - index); size += csize; for ( ; index < end; index++) data[index] = itr.next(); return csize > 0; } /** * Removes all elements in the half-open interval [fromIndex, toIndex). * Does nothing when toIndex is equal to fromIndex. * * @param fromIndex the first index which will be removed * @param toIndex one greater than the last index which will be removed * @throws IndexOutOfBoundsException if fromIndex > toIndex */ protected void removeRange(int fromIndex, int toIndex) { int change = toIndex - fromIndex; if (change > 0) { modCount++; System.arraycopy(data, toIndex, data, fromIndex, size - toIndex); size -= change; } else if (change < 0) throw new IndexOutOfBoundsException(); } /** * Checks that the index is in the range of possible elements (inclusive). * * @param index the index to check * @throws IndexOutOfBoundsException if index > size */ private void checkBoundInclusive(int index) { // Implementation note: we do not check for negative ranges here, since // use of a negative index will cause an ArrayIndexOutOfBoundsException, // a subclass of the required exception, with no effort on our part. if (index > size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } /** * Checks that the index is in the range of existing elements (exclusive). * * @param index the index to check * @throws IndexOutOfBoundsException if index >= size */ private void checkBoundExclusive(int index) { // Implementation note: we do not check for negative ranges here, since // use of a negative index will cause an ArrayIndexOutOfBoundsException, // a subclass of the required exception, with no effort on our part. if (index >= size) throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size); } /** * Remove from this list all elements contained in the given collection. * This is not public, due to Sun's API, but this performs in linear * time while the default behavior of AbstractList would be quadratic. * * @param c the collection to filter out * @return true if this list changed * @throws NullPointerException if c is null */ boolean removeAllInternal(Collection c) { int i; int j; for (i = 0; i < size; i++) if (c.contains(data[i])) break; if (i == size) return false; modCount++; for (j = i++; i < size; i++) if (! c.contains(data[i])) data[j++] = data[i]; size -= i - j; return true; } /** * Retain in this vector only the elements contained in the given collection. * This is not public, due to Sun's API, but this performs in linear * time while the default behavior of AbstractList would be quadratic. * * @param c the collection to filter by * @return true if this vector changed * @throws NullPointerException if c is null * @since 1.2 */ boolean retainAllInternal(Collection c) { int i; int j; for (i = 0; i < size; i++) if (! c.contains(data[i])) break; if (i == size) return false; modCount++; for (j = i++; i < size; i++) if (c.contains(data[i])) data[j++] = data[i]; size -= i - j; return true; } /** * Serializes this object to the given stream. * * @param out the stream to write to * @throws IOException if the underlying stream fails * @serialData the size field (int), the length of the backing array * (int), followed by its elements (Objects) in proper order. */ private void writeObject(ObjectOutputStream s) throws IOException { // The 'size' field. s.defaultWriteObject(); // We serialize unused list entries to preserve capacity. int len = data.length; s.writeInt(len); // it would be more efficient to just write "size" items, // this need readObject read "size" items too. for (int i = 0; i < size; i++) s.writeObject(data[i]); } /** * Deserializes this object from the given stream. * * @param in the stream to read from * @throws ClassNotFoundException if the underlying stream fails * @throws IOException if the underlying stream fails * @serialData the size field (int), the length of the backing array * (int), followed by its elements (Objects) in proper order. */ private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException { // the `size' field. s.defaultReadObject(); int capacity = s.readInt(); data = new Object[capacity]; for (int i = 0; i < size; i++) data[i] = s.readObject(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -