Java The Complete Reference, Seventh Edition

(Greg DeLong) #1

Chapter 17: java.util Part 1: The Collections Framework 481


static int[ ] copyOf(int[ ]source, intlen)
static long[ ] copyOf(long[ ]source, intlen)
static short[ ] copyOf(short[ ]source, intlen)
static <T> T[ ] copyOf(T[ ]source, intlen)
static <T,U> T[ ] copyOf(U[ ]source, intlen, Class<? extends T[ ]>resultT)

The original array is specified bysource,and the length of the copy is specified bylen. If the
copy is longer thansource, then the copy is padded with zeros (for numeric arrays),nulls
(for object arrays), orfalse(for boolean arrays). If the copy is shorter thansource, then the
copy is truncated. In the last form, the type ofresultTbecomes the type of the array
returned. Iflenis negative, aNegativeArraySizeExceptionis thrown. Ifsourceisnull,
aNullPointerExceptionis thrown. IfresultTis incompatible with the type ofsource,an
ArrayStoreExceptionis thrown.
ThecopyOfRange( )method was also added by Java SE 6. It returns a copy of a range
within an array and has the following forms:


static boolean[ ] copyOfRange(boolean[ ]source, intstart, intend)
static byte[ ] copyOfRange(byte[ ]source, intstart, intend)
static char[ ] copyOfRange(char[ ]source, intstart, intend)
static double[ ] copyOfRange(double[ ]source, intstart, intend)
static float[ ] copyOfRange(float[ ]source, intstart, intend)
static int[ ] copyOfRange(int[ ]source, intstart, intend)
static long[ ] copyOfRange(long[ ]source, intstart, intend)
static short[ ] copyOfRange(short[ ]source, intstart, intend)
static <T> T[ ] copyOfRange(T[ ]source, intstart, intend)
static <T,U> T[ ] copyOfRange(U[ ]source, intstart, intend,
Class<? extends T[ ]>resultT)

The original array is specified bysource. The range to copy is specified by the indices
passed viastartandend. The range runs fromstarttoend–1. If the range is longer than source,
then the copy is padded with zeros (for numeric arrays),nulls (for object arrays), orfalse(for
boolean arrays). In the last form, the type ofresultTbecomes the type of the array returned. If
startis negative or greater than the length ofsource,anArrayIndexOutOfBoundsExceptionis
thrown. Ifstartis greater thanend,anIllegalArgumentExceptionis thrown. Ifsourceisnull,a
NullPointerExceptionis thrown. IfresultTis incompatible with the type ofsource,an
ArrayStoreExceptionis thrown.
Theequals( )method returnstrueif two arrays are equivalent. Otherwise, it returnsfalse.
Theequals( )method has the following forms:


static boolean equals(booleanarray1[ ], booleanarray2[ ])
static boolean equals(bytearray1[ ], bytearray2[ ])
static boolean equals(chararray1[ ], chararray2[ ])
static boolean equals(doublearray1[ ], doublearray2[ ])
static boolean equals(floatarray1[ ], floatarray2[ ])
static boolean equals(intarray1[ ], intarray2[ ])
static boolean equals(longarray1[ ], longarray2[ ])
static boolean equals(shortarray1[ ], shortarray2[ ])
static boolean equals(Objectarray1[ ], Objectarray2[ ])

Here,array1andarray2are the two arrays that are compared for equality.

Free download pdf