796 public <T> T[] toArray(T[] a) {
797 int size = size();
798 if (a.length < size)
799 a = (T[])java.lang.reflect.Array.newInstance(
800 a.getClass().getComponentType(), size);
801 copyElements(a);
802 if (a.length > size)
803 a[size] = null;
804 return a;
805 }
806
807 // *** Object methods ***
808
809 /**
810 * Returns a copy of this deque.
811 *
812 * @return a copy of this deque
813 */
814 public ArrayDeque<E> clone() {
815 try {
816 ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
817 result.elements = Arrays.copyOf(elements, elements.length);
818 return result;
819
820 } catch (CloneNotSupportedException e) {
821 throw new AssertionError();
822 }
823 }
824
825 /**
826 * Appease the serialization gods.
827 */
828 private static final long serialVersionUID = 2340985798034038923L;
829
830 /**
831 * Serialize this deque.
832 *
833 * @serialData The current size (<tt>int</tt>) of the deque,
834 * followed by all of its elements (each an object reference) in
835 * first-to-last order.
836 */
837 private void writeObject(ObjectOutputStream s) throws IOException {
838 s.defaultWriteObject();
839
840 // Write out size
841 s.writeInt(size());
842
843 // Write out elements in order.
844 int mask = elements.length - 1;
845 for (int i = head; i != tail; i = (i + 1) & mask)
846 s.writeObject(elements[i]);
847 }
848
849 /**
850 * Deserialize this deque.
851 */
852 private void readObject(ObjectInputStream s)
853 throws IOException, ClassNotFoundException {
854 s.defaultReadObject();
855
856 // Read in size and allocate array
857 int size = s.readInt();
858 allocateElements(size);
859 head = 0;
860 tail = size;
861
862 // Read in all elements in the proper order.
863 for (int i = 0; i < size; i++)
864 elements[i] = (E)s.readObject();
865 }
866 }
|
796 public <T> T[] toArray(T[] a) {
797 int size = size();
798 if (a.length < size)
799 a = (T[])java.lang.reflect.Array.newInstance(
800 a.getClass().getComponentType(), size);
801 copyElements(a);
802 if (a.length > size)
803 a[size] = null;
804 return a;
805 }
806
807 // *** Object methods ***
808
809 /**
810 * Returns a copy of this deque.
811 *
812 * @return a copy of this deque
813 */
814 public ArrayDeque<E> clone() {
815 try {
816 @SuppressWarnings("unchecked")
817 ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
818 result.elements = Arrays.copyOf(elements, elements.length);
819 return result;
820
821 } catch (CloneNotSupportedException e) {
822 throw new AssertionError();
823 }
824 }
825
826 /**
827 * Appease the serialization gods.
828 */
829 private static final long serialVersionUID = 2340985798034038923L;
830
831 /**
832 * Serialize this deque.
833 *
834 * @serialData The current size (<tt>int</tt>) of the deque,
835 * followed by all of its elements (each an object reference) in
836 * first-to-last order.
837 */
838 private void writeObject(ObjectOutputStream s) throws IOException {
839 s.defaultWriteObject();
840
841 // Write out size
842 s.writeInt(size());
843
844 // Write out elements in order.
845 int mask = elements.length - 1;
846 for (int i = head; i != tail; i = (i + 1) & mask)
847 s.writeObject(elements[i]);
848 }
849
850 /**
851 * Deserialize this deque.
852 */
853 @SuppressWarnings("unchecked")
854 private void readObject(ObjectInputStream s)
855 throws IOException, ClassNotFoundException {
856 s.defaultReadObject();
857
858 // Read in size and allocate array
859 int size = s.readInt();
860 allocateElements(size);
861 head = 0;
862 tail = size;
863
864 // Read in all elements in the proper order.
865 for (int i = 0; i < size; i++)
866 elements[i] = (E)s.readObject();
867 }
868 }
|