📄 unorderlist.java
字号:
return (Object[])java.lang.reflect.Array.newInstance(componentType, 0); } int s = size - idx; Object data[] = (Object[])java.lang.reflect.Array.newInstance(componentType, s); System.arraycopy(elementData, idx, data, 0, s); return data; } // copy element to objs and clear the array synchronized final void toArrayAndClear(Object[] objs) { System.arraycopy(elementData, 0, objs, 0, size); Arrays.fill(elementData, 0, size, null); size = 0; isDirty = true; } /** * Trims the capacity of this <tt>ArrayList</tt> instance to be the * list's current size. An application can use this operation to minimize * the storage of an <tt>ArrayList</tt> instance. */ synchronized final void trimToSize() { if (elementData.length > size) { Object oldData[] = elementData; elementData = (Object[])java.lang.reflect.Array.newInstance( componentType, size); System.arraycopy(oldData, 0, elementData, 0, size); } } // Positional Access Operations /** * Returns the element at the specified position in this list. * * @param index index of element to return. * @return the element at the specified position in this list. * @throws IndexOutOfBoundsException if index is out of range <tt>(index * < 0 || index >= size())</tt>. */ synchronized final Object get(int index) { return elementData[index]; } /** * Replaces the element at the specified position in this list with * the specified element. * * @param index index of element to replace. * @param element element to be stored at the specified position. * @return the element previously at the specified position. * @throws IndexOutOfBoundsException if index out of range * <tt>(index < 0 || index >= size())</tt>. */ synchronized final void set(int index, Object element) { elementData[index] = element; isDirty = true; } /** * Appends the specified element to the end of this list. * It is the user responsible to ensure that the element add is of * the same type as array componentType. * * @param o element to be appended to this list. */ synchronized final void add(Object o) { if (elementData.length == size) { Object oldData[] = elementData; elementData = (Object[])java.lang.reflect.Array.newInstance( componentType, (size << 1)); System.arraycopy(oldData, 0, elementData, 0, size); } elementData[size++] = o; isDirty = true; } /** * Removes the element at the specified position in this list. * Replace the removed element by the last one. * * @param index the index of the element to removed. * @throws IndexOutOfBoundsException if index out of range <tt>(index * < 0 || index >= size())</tt>. */ synchronized final void remove(int index) { elementData[index] = elementData[--size]; elementData[size] = null; isDirty = true; /* if ((cloneData != null) && (index < cloneData.length)) { cloneData[index] = null; // for gc } */ } /** * Removes the element at the specified position in this list. * The order is keep. * * @param index the index of the element to removed. * @throws IndexOutOfBoundsException if index out of range <tt>(index * < 0 || index >= size())</tt>. */ synchronized final void removeOrdered(int index) { size--; if (index < size) { System.arraycopy(elementData, index+1, elementData, index, size-index); } // gc for last element elementData[size] = null; isDirty = true; } /** * Removes the element at the last position in this list. * @return The element remove * @throws IndexOutOfBoundsException if array is empty */ synchronized final Object removeLastElement() { Object elm = elementData[--size]; elementData[size] = null; isDirty = true; /* if ((cloneData != null) && (size < cloneData.length)) { cloneData[size] = null; // for gc } */ return elm; } // Shift element of array from positin idx to position 0 // Note that idx < size, otherwise ArrayIndexOutOfBoundsException // throws. The element remove are copy to objs. synchronized final void shift(Object objs[], int idx) { int oldsize = size; System.arraycopy(elementData, 0, objs, 0, idx); size -= idx; if (size > 0) { System.arraycopy(elementData, idx, elementData, 0, size); } Arrays.fill(elementData, size, oldsize, null); } /** * Removes the specified element in this list. * Replace the removed element by the last one. * * @param o the element to removed. * @return true if object remove * @throws IndexOutOfBoundsException if index out of range <tt>(index * < 0 || index >= size())</tt>. */ synchronized final boolean remove(Object o) { size--; if (o != null) { for (int i=size; i >= 0; i--) { if (o.equals(elementData[i])) { elementData[i] = elementData[size]; elementData[size] = null; /* if ((cloneData != null) && (i < cloneData.length)) { cloneData[i] = null; // for gc } */ isDirty = true; return true; } } } else { for (int i=size; i >= 0; i--) if (elementData[i]==null) { elementData[i] = elementData[size]; elementData[size] = null; /* if ((cloneData != null) && (i < cloneData.length)) { cloneData[i] = null; // for gc } */ isDirty = true; return true; } } size++; // fail to remove return false; } /** * Removes all of the elements from this list. The list will * be empty after this call returns. */ synchronized final void clear() { if (size > 0) { Arrays.fill(elementData, 0, size, null); size = 0; isDirty = true; } } synchronized final void clearMirror() { if (cloneData != null) { Arrays.fill(cloneData, 0, cloneData.length, null); } cloneSize = 0; isDirty = true; } final Class getComponentType() { return componentType; } synchronized public String toString() { StringBuffer sb = new StringBuffer("Size = " + size + "\n["); int len = size-1; Object obj; for (int i=0; i < size; i++) { obj = elementData[i]; if (obj != null) { sb.append(elementData[i].toString()); } else { sb.append("NULL"); } if (i != len) { sb.append(", "); } } sb.append("]\n"); return sb.toString(); } /** * Save the state of the <tt>ArrayList</tt> instance to a stream (that * is, serialize it). * * @serialData The length of the array backing the <tt>ArrayList</tt> * instance is emitted (int), followed by all of its elements * (each an <tt>Object</tt>) in the proper order. */ private synchronized void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff s.defaultWriteObject(); // Write out array length s.writeInt(elementData.length); // Write out all elements in the proper order. for (int i=0; i<size; i++) s.writeObject(elementData[i]); } /** * Reconstitute the <tt>ArrayList</tt> instance from a stream (that is, * deserialize it). */ private synchronized void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in size, and any hidden stuff s.defaultReadObject(); // Read in array length and allocate array int arrayLength = s.readInt(); elementData = (Object[])java.lang.reflect.Array.newInstance( componentType, arrayLength); // Read in all elements in the proper order. for (int i=0; i<size; i++) elementData[i] = s.readObject(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -