92 * exception for its correctness: <i>the fail-fast behavior of iterators
93 * should be used only to detect bugs.</i>
94 *
95 * <p>This class is a member of the
96 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
97 * Java Collections Framework</a>.
98 *
99 * @param <E> the type of elements in this list
100 *
101 * @author Josh Bloch
102 * @author Neal Gafter
103 * @see Collection
104 * @see List
105 * @see LinkedList
106 * @see Vector
107 * @since 1.2
108 */
109 public class ArrayList<E> extends AbstractList<E>
110 implements List<E>, RandomAccess, Cloneable, java.io.Serializable
111 {
112 private static final long serialVersionUID = 8683452581122892189L;
113
114 /**
115 * Default initial capacity.
116 */
117 private static final int DEFAULT_CAPACITY = 10;
118
119 /**
120 * Shared empty array instance used for empty instances.
121 */
122 private static final Object[] EMPTY_ELEMENTDATA = {};
123
124 /**
125 * Shared empty array instance used for default sized empty instances. We
126 * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
127 * first element is added.
128 */
129 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
130
131 /**
832 System.arraycopy(es, r, es, w, end - r);
833 w += end - r;
834 throw ex;
835 } finally {
836 modCount += end - w;
837 shiftTailOverGap(es, w, end);
838 }
839 return true;
840 }
841
842 /**
843 * Saves the state of the {@code ArrayList} instance to a stream
844 * (that is, serializes it).
845 *
846 * @param s the stream
847 * @throws java.io.IOException if an I/O error occurs
848 * @serialData The length of the array backing the {@code ArrayList}
849 * instance is emitted (int), followed by all of its elements
850 * (each an {@code Object}) in the proper order.
851 */
852 private void writeObject(java.io.ObjectOutputStream s)
853 throws java.io.IOException {
854 // Write out element count, and any hidden stuff
855 int expectedModCount = modCount;
856 s.defaultWriteObject();
857
858 // Write out size as capacity for behavioral compatibility with clone()
859 s.writeInt(size);
860
861 // Write out all elements in the proper order.
862 for (int i=0; i<size; i++) {
863 s.writeObject(elementData[i]);
864 }
865
866 if (modCount != expectedModCount) {
867 throw new ConcurrentModificationException();
868 }
869 }
870
871 /**
872 * Reconstitutes the {@code ArrayList} instance from a stream (that is,
873 * deserializes it).
874 * @param s the stream
875 * @throws ClassNotFoundException if the class of a serialized object
876 * could not be found
877 * @throws java.io.IOException if an I/O error occurs
878 */
879 private void readObject(java.io.ObjectInputStream s)
880 throws java.io.IOException, ClassNotFoundException {
881
882 // Read in size, and any hidden stuff
883 s.defaultReadObject();
884
885 // Read in capacity
886 s.readInt(); // ignored
887
888 if (size > 0) {
889 // like clone(), allocate array based upon size not capacity
890 SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
891 Object[] elements = new Object[size];
892
893 // Read in all elements in the proper order.
894 for (int i = 0; i < size; i++) {
895 elements[i] = s.readObject();
896 }
897
898 elementData = elements;
|
92 * exception for its correctness: <i>the fail-fast behavior of iterators
93 * should be used only to detect bugs.</i>
94 *
95 * <p>This class is a member of the
96 * <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">
97 * Java Collections Framework</a>.
98 *
99 * @param <E> the type of elements in this list
100 *
101 * @author Josh Bloch
102 * @author Neal Gafter
103 * @see Collection
104 * @see List
105 * @see LinkedList
106 * @see Vector
107 * @since 1.2
108 */
109 public class ArrayList<E> extends AbstractList<E>
110 implements List<E>, RandomAccess, Cloneable, java.io.Serializable
111 {
112 @java.io.Serial
113 private static final long serialVersionUID = 8683452581122892189L;
114
115 /**
116 * Default initial capacity.
117 */
118 private static final int DEFAULT_CAPACITY = 10;
119
120 /**
121 * Shared empty array instance used for empty instances.
122 */
123 private static final Object[] EMPTY_ELEMENTDATA = {};
124
125 /**
126 * Shared empty array instance used for default sized empty instances. We
127 * distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
128 * first element is added.
129 */
130 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
131
132 /**
833 System.arraycopy(es, r, es, w, end - r);
834 w += end - r;
835 throw ex;
836 } finally {
837 modCount += end - w;
838 shiftTailOverGap(es, w, end);
839 }
840 return true;
841 }
842
843 /**
844 * Saves the state of the {@code ArrayList} instance to a stream
845 * (that is, serializes it).
846 *
847 * @param s the stream
848 * @throws java.io.IOException if an I/O error occurs
849 * @serialData The length of the array backing the {@code ArrayList}
850 * instance is emitted (int), followed by all of its elements
851 * (each an {@code Object}) in the proper order.
852 */
853 @java.io.Serial
854 private void writeObject(java.io.ObjectOutputStream s)
855 throws java.io.IOException {
856 // Write out element count, and any hidden stuff
857 int expectedModCount = modCount;
858 s.defaultWriteObject();
859
860 // Write out size as capacity for behavioral compatibility with clone()
861 s.writeInt(size);
862
863 // Write out all elements in the proper order.
864 for (int i=0; i<size; i++) {
865 s.writeObject(elementData[i]);
866 }
867
868 if (modCount != expectedModCount) {
869 throw new ConcurrentModificationException();
870 }
871 }
872
873 /**
874 * Reconstitutes the {@code ArrayList} instance from a stream (that is,
875 * deserializes it).
876 * @param s the stream
877 * @throws ClassNotFoundException if the class of a serialized object
878 * could not be found
879 * @throws java.io.IOException if an I/O error occurs
880 */
881 @java.io.Serial
882 private void readObject(java.io.ObjectInputStream s)
883 throws java.io.IOException, ClassNotFoundException {
884
885 // Read in size, and any hidden stuff
886 s.defaultReadObject();
887
888 // Read in capacity
889 s.readInt(); // ignored
890
891 if (size > 0) {
892 // like clone(), allocate array based upon size not capacity
893 SharedSecrets.getJavaObjectInputStreamAccess().checkArray(s, Object[].class, size);
894 Object[] elements = new Object[size];
895
896 // Read in all elements in the proper order.
897 for (int i = 0; i < size; i++) {
898 elements[i] = s.readObject();
899 }
900
901 elementData = elements;
|