< prev index next >

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

Print this page




1095      *         is not a supertype of the runtime type of every element in
1096      *         this list
1097      * @throws NullPointerException if the specified array is null
1098      */
1099     @SuppressWarnings("unchecked")
1100     public <T> T[] toArray(T[] a) {
1101         if (a.length < size)
1102             a = (T[])java.lang.reflect.Array.newInstance(
1103                                 a.getClass().getComponentType(), size);
1104         int i = 0;
1105         Object[] result = a;
1106         for (Node<E> x = first; x != null; x = x.next)
1107             result[i++] = x.item;
1108 
1109         if (a.length > size)
1110             a[size] = null;
1111 
1112         return a;
1113     }
1114 

1115     private static final long serialVersionUID = 876323262645176354L;
1116 
1117     /**
1118      * Saves the state of this {@code LinkedList} instance to a stream
1119      * (that is, serializes it).
1120      *
1121      * @serialData The size of the list (the number of elements it
1122      *             contains) is emitted (int), followed by all of its
1123      *             elements (each an Object) in the proper order.
1124      */

1125     private void writeObject(java.io.ObjectOutputStream s)
1126         throws java.io.IOException {
1127         // Write out any hidden serialization magic
1128         s.defaultWriteObject();
1129 
1130         // Write out size
1131         s.writeInt(size);
1132 
1133         // Write out all elements in the proper order.
1134         for (Node<E> x = first; x != null; x = x.next)
1135             s.writeObject(x.item);
1136     }
1137 
1138     /**
1139      * Reconstitutes this {@code LinkedList} instance from a stream
1140      * (that is, deserializes it).
1141      */
1142     @SuppressWarnings("unchecked")

1143     private void readObject(java.io.ObjectInputStream s)
1144         throws java.io.IOException, ClassNotFoundException {
1145         // Read in any hidden serialization magic
1146         s.defaultReadObject();
1147 
1148         // Read in size
1149         int size = s.readInt();
1150 
1151         // Read in all elements in the proper order.
1152         for (int i = 0; i < size; i++)
1153             linkLast((E)s.readObject());
1154     }
1155 
1156     /**
1157      * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
1158      * and <em>fail-fast</em> {@link Spliterator} over the elements in this
1159      * list.
1160      *
1161      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
1162      * {@link Spliterator#ORDERED}.  Overriding implementations should document




1095      *         is not a supertype of the runtime type of every element in
1096      *         this list
1097      * @throws NullPointerException if the specified array is null
1098      */
1099     @SuppressWarnings("unchecked")
1100     public <T> T[] toArray(T[] a) {
1101         if (a.length < size)
1102             a = (T[])java.lang.reflect.Array.newInstance(
1103                                 a.getClass().getComponentType(), size);
1104         int i = 0;
1105         Object[] result = a;
1106         for (Node<E> x = first; x != null; x = x.next)
1107             result[i++] = x.item;
1108 
1109         if (a.length > size)
1110             a[size] = null;
1111 
1112         return a;
1113     }
1114 
1115     @java.io.Serial
1116     private static final long serialVersionUID = 876323262645176354L;
1117 
1118     /**
1119      * Saves the state of this {@code LinkedList} instance to a stream
1120      * (that is, serializes it).
1121      *
1122      * @serialData The size of the list (the number of elements it
1123      *             contains) is emitted (int), followed by all of its
1124      *             elements (each an Object) in the proper order.
1125      */
1126     @java.io.Serial
1127     private void writeObject(java.io.ObjectOutputStream s)
1128         throws java.io.IOException {
1129         // Write out any hidden serialization magic
1130         s.defaultWriteObject();
1131 
1132         // Write out size
1133         s.writeInt(size);
1134 
1135         // Write out all elements in the proper order.
1136         for (Node<E> x = first; x != null; x = x.next)
1137             s.writeObject(x.item);
1138     }
1139 
1140     /**
1141      * Reconstitutes this {@code LinkedList} instance from a stream
1142      * (that is, deserializes it).
1143      */
1144     @SuppressWarnings("unchecked")
1145     @java.io.Serial
1146     private void readObject(java.io.ObjectInputStream s)
1147         throws java.io.IOException, ClassNotFoundException {
1148         // Read in any hidden serialization magic
1149         s.defaultReadObject();
1150 
1151         // Read in size
1152         int size = s.readInt();
1153 
1154         // Read in all elements in the proper order.
1155         for (int i = 0; i < size; i++)
1156             linkLast((E)s.readObject());
1157     }
1158 
1159     /**
1160      * Creates a <em><a href="Spliterator.html#binding">late-binding</a></em>
1161      * and <em>fail-fast</em> {@link Spliterator} over the elements in this
1162      * list.
1163      *
1164      * <p>The {@code Spliterator} reports {@link Spliterator#SIZED} and
1165      * {@link Spliterator#ORDERED}.  Overriding implementations should document


< prev index next >