< prev index next >

src/java.base/share/classes/java/util/Vector.java

Print this page




 107     /**
 108      * The number of valid components in this {@code Vector} object.
 109      * Components {@code elementData[0]} through
 110      * {@code elementData[elementCount-1]} are the actual items.
 111      *
 112      * @serial
 113      */
 114     protected int elementCount;
 115 
 116     /**
 117      * The amount by which the capacity of the vector is automatically
 118      * incremented when its size becomes greater than its capacity.  If
 119      * the capacity increment is less than or equal to zero, the capacity
 120      * of the vector is doubled each time it needs to grow.
 121      *
 122      * @serial
 123      */
 124     protected int capacityIncrement;
 125 
 126     /** use serialVersionUID from JDK 1.0.2 for interoperability */

 127     private static final long serialVersionUID = -2767605614048989439L;
 128 
 129     /**
 130      * Constructs an empty vector with the specified initial capacity and
 131      * capacity increment.
 132      *
 133      * @param   initialCapacity     the initial capacity of the vector
 134      * @param   capacityIncrement   the amount by which the capacity is
 135      *                              increased when the vector overflows
 136      * @throws IllegalArgumentException if the specified initial capacity
 137      *         is negative
 138      */
 139     public Vector(int initialCapacity, int capacityIncrement) {
 140         super();
 141         if (initialCapacity < 0)
 142             throw new IllegalArgumentException("Illegal Capacity: "+
 143                                                initialCapacity);
 144         this.elementData = new Object[initialCapacity];
 145         this.capacityIncrement = capacityIncrement;
 146     }


1132     }
1133 
1134     /** Erases the gap from lo to hi, by sliding down following elements. */
1135     private void shiftTailOverGap(Object[] es, int lo, int hi) {
1136         System.arraycopy(es, hi, es, lo, elementCount - hi);
1137         for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1138             es[i] = null;
1139     }
1140 
1141     /**
1142      * Loads a {@code Vector} instance from a stream
1143      * (that is, deserializes it).
1144      * This method performs checks to ensure the consistency
1145      * of the fields.
1146      *
1147      * @param in the stream
1148      * @throws java.io.IOException if an I/O error occurs
1149      * @throws ClassNotFoundException if the stream contains data
1150      *         of a non-existing class
1151      */

1152     private void readObject(ObjectInputStream in)
1153             throws IOException, ClassNotFoundException {
1154         ObjectInputStream.GetField gfields = in.readFields();
1155         int count = gfields.get("elementCount", 0);
1156         Object[] data = (Object[])gfields.get("elementData", null);
1157         if (count < 0 || data == null || count > data.length) {
1158             throw new StreamCorruptedException("Inconsistent vector internals");
1159         }
1160         elementCount = count;
1161         elementData = data.clone();
1162     }
1163 
1164     /**
1165      * Saves the state of the {@code Vector} instance to a stream
1166      * (that is, serializes it).
1167      * This method performs synchronization to ensure the consistency
1168      * of the serialized data.
1169      *
1170      * @param s the stream
1171      * @throws java.io.IOException if an I/O error occurs
1172      */

1173     private void writeObject(java.io.ObjectOutputStream s)
1174             throws java.io.IOException {
1175         final java.io.ObjectOutputStream.PutField fields = s.putFields();
1176         final Object[] data;
1177         synchronized (this) {
1178             fields.put("capacityIncrement", capacityIncrement);
1179             fields.put("elementCount", elementCount);
1180             data = elementData.clone();
1181         }
1182         fields.put("elementData", data);
1183         s.writeFields();
1184     }
1185 
1186     /**
1187      * Returns a list iterator over the elements in this list (in proper
1188      * sequence), starting at the specified position in the list.
1189      * The specified index indicates the first element that would be
1190      * returned by an initial call to {@link ListIterator#next next}.
1191      * An initial call to {@link ListIterator#previous previous} would
1192      * return the element with the specified index minus one.




 107     /**
 108      * The number of valid components in this {@code Vector} object.
 109      * Components {@code elementData[0]} through
 110      * {@code elementData[elementCount-1]} are the actual items.
 111      *
 112      * @serial
 113      */
 114     protected int elementCount;
 115 
 116     /**
 117      * The amount by which the capacity of the vector is automatically
 118      * incremented when its size becomes greater than its capacity.  If
 119      * the capacity increment is less than or equal to zero, the capacity
 120      * of the vector is doubled each time it needs to grow.
 121      *
 122      * @serial
 123      */
 124     protected int capacityIncrement;
 125 
 126     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 127     @java.io.Serial
 128     private static final long serialVersionUID = -2767605614048989439L;
 129 
 130     /**
 131      * Constructs an empty vector with the specified initial capacity and
 132      * capacity increment.
 133      *
 134      * @param   initialCapacity     the initial capacity of the vector
 135      * @param   capacityIncrement   the amount by which the capacity is
 136      *                              increased when the vector overflows
 137      * @throws IllegalArgumentException if the specified initial capacity
 138      *         is negative
 139      */
 140     public Vector(int initialCapacity, int capacityIncrement) {
 141         super();
 142         if (initialCapacity < 0)
 143             throw new IllegalArgumentException("Illegal Capacity: "+
 144                                                initialCapacity);
 145         this.elementData = new Object[initialCapacity];
 146         this.capacityIncrement = capacityIncrement;
 147     }


1133     }
1134 
1135     /** Erases the gap from lo to hi, by sliding down following elements. */
1136     private void shiftTailOverGap(Object[] es, int lo, int hi) {
1137         System.arraycopy(es, hi, es, lo, elementCount - hi);
1138         for (int to = elementCount, i = (elementCount -= hi - lo); i < to; i++)
1139             es[i] = null;
1140     }
1141 
1142     /**
1143      * Loads a {@code Vector} instance from a stream
1144      * (that is, deserializes it).
1145      * This method performs checks to ensure the consistency
1146      * of the fields.
1147      *
1148      * @param in the stream
1149      * @throws java.io.IOException if an I/O error occurs
1150      * @throws ClassNotFoundException if the stream contains data
1151      *         of a non-existing class
1152      */
1153     @java.io.Serial
1154     private void readObject(ObjectInputStream in)
1155             throws IOException, ClassNotFoundException {
1156         ObjectInputStream.GetField gfields = in.readFields();
1157         int count = gfields.get("elementCount", 0);
1158         Object[] data = (Object[])gfields.get("elementData", null);
1159         if (count < 0 || data == null || count > data.length) {
1160             throw new StreamCorruptedException("Inconsistent vector internals");
1161         }
1162         elementCount = count;
1163         elementData = data.clone();
1164     }
1165 
1166     /**
1167      * Saves the state of the {@code Vector} instance to a stream
1168      * (that is, serializes it).
1169      * This method performs synchronization to ensure the consistency
1170      * of the serialized data.
1171      *
1172      * @param s the stream
1173      * @throws java.io.IOException if an I/O error occurs
1174      */
1175     @java.io.Serial
1176     private void writeObject(java.io.ObjectOutputStream s)
1177             throws java.io.IOException {
1178         final java.io.ObjectOutputStream.PutField fields = s.putFields();
1179         final Object[] data;
1180         synchronized (this) {
1181             fields.put("capacityIncrement", capacityIncrement);
1182             fields.put("elementCount", elementCount);
1183             data = elementData.clone();
1184         }
1185         fields.put("elementData", data);
1186         s.writeFields();
1187     }
1188 
1189     /**
1190      * Returns a list iterator over the elements in this list (in proper
1191      * sequence), starting at the specified position in the list.
1192      * The specified index indicates the first element that would be
1193      * returned by an initial call to {@link ListIterator#next next}.
1194      * An initial call to {@link ListIterator#previous previous} would
1195      * return the element with the specified index minus one.


< prev index next >