/* Arrays.java -- Utility class with methods to operate on arrays Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package java.util; import gnu.java.lang.CPStringBuilder; import java.io.Serializable; import java.lang.reflect.Array; /** * This class contains various static utility methods performing operations on * arrays, and a method to provide a List "view" of an array to facilitate * using arrays with Collection-based APIs. All methods throw a * {@link NullPointerException} if the parameter array is null. *
*
* Implementations may use their own algorithms, but must obey the general
* properties; for example, the sort must be stable and n*log(n) complexity.
* Sun's implementation of sort, and therefore ours, is a tuned quicksort,
* adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort
* Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265
* (November 1993). This algorithm offers n*log(n) performance on many data
* sets that cause other quicksorts to degrade to quadratic performance.
*
* @author Original author unknown
* @author Bryce McKinlay
* @author Eric Blake (ebb9@email.byu.edu)
* @see Comparable
* @see Comparator
* @since 1.2
* @status updated to 1.4
*/
public class Arrays
{
/**
* This class is non-instantiable.
*/
private Arrays()
{
}
// binarySearch
/**
* Perform a binary search of a byte array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(byte[] a, byte key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a byte array for a key. The range
* must be sorted (as by the sort(byte[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(byte[] a, int low, int hi, byte key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final byte d = a[mid];
if (d == key)
return mid;
else if (d > key)
hi = mid - 1;
else
// This gets the insertion point right on the last loop.
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of a char array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(char[] a, char key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a char array for a key. The range
* must be sorted (as by the sort(char[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(char[] a, int low, int hi, char key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final char d = a[mid];
if (d == key)
return mid;
else if (d > key)
hi = mid - 1;
else
// This gets the insertion point right on the last loop.
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of a short array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(short[] a, short key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a short array for a key. The range
* must be sorted (as by the sort(short[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(short[] a, int low, int hi, short key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final short d = a[mid];
if (d == key)
return mid;
else if (d > key)
hi = mid - 1;
else
// This gets the insertion point right on the last loop.
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of an int array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(int[] a, int key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of an integer array for a key. The range
* must be sorted (as by the sort(int[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(int[] a, int low, int hi, int key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final int d = a[mid];
if (d == key)
return mid;
else if (d > key)
hi = mid - 1;
else
// This gets the insertion point right on the last loop.
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of a long array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(long[] a, long key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a long array for a key. The range
* must be sorted (as by the sort(long[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(long[] a, int low, int hi, long key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final long d = a[mid];
if (d == key)
return mid;
else if (d > key)
hi = mid - 1;
else
// This gets the insertion point right on the last loop.
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of a float array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(float[] a, float key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a float array for a key. The range
* must be sorted (as by the sort(float[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(float[] a, int low, int hi, float key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
// Must use Float.compare to take into account NaN, +-0.
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final int r = Float.compare(a[mid], key);
if (r == 0)
return mid;
else if (r > 0)
hi = mid - 1;
else
// This gets the insertion point right on the last loop
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of a double array for a key. The array must be
* sorted (as by the sort() method) - if it is not, the behaviour of this
* method is undefined, and may be an infinite loop. If the array contains
* the key more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(double[] a, double key)
{
if (a.length == 0)
return -1;
return binarySearch(a, 0, a.length - 1, key);
}
/**
* Perform a binary search of a range of a double array for a key. The range
* must be sorted (as by the sort(double[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static int binarySearch(double[] a, int low, int hi, double key)
{
if (low > hi)
throw new IllegalArgumentException("The start index is higher than " +
"the finish index.");
if (low < 0 || hi > a.length)
throw new ArrayIndexOutOfBoundsException("One of the indices is out " +
"of bounds.");
// Must use Double.compare to take into account NaN, +-0.
int mid = 0;
while (low <= hi)
{
mid = (low + hi) >>> 1;
final int r = Double.compare(a[mid], key);
if (r == 0)
return mid;
else if (r > 0)
hi = mid - 1;
else
// This gets the insertion point right on the last loop
low = ++mid;
}
return -mid - 1;
}
/**
* Perform a binary search of an Object array for a key, using the natural
* ordering of the elements. The array must be sorted (as by the sort()
* method) - if it is not, the behaviour of this method is undefined, and may
* be an infinite loop. Further, the key must be comparable with every item
* in the array. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this (JCL)
* implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws ClassCastException if key could not be compared with one of the
* elements of a
* @throws NullPointerException if a null element in a is compared
*/
public static int binarySearch(Object[] a, Object key)
{
if (a.length == 0)
return -1;
return binarySearch(a, key, null);
}
/**
* Perform a binary search of a range of an Object array for a key. The range
* must be sorted (as by the sort(Object[], int, int)
method) -
* if it is not, the behaviour of this method is undefined, and may be an
* infinite loop. If the array contains the key more than once, any one of
* them may be found. Note: although the specification allows for an infinite
* loop if the array is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
*/
public static int binarySearch(Object[] a, int low, int hi, Object key)
{
return binarySearch(a, low, hi, key, null);
}
/**
* Perform a binary search of an Object array for a key, using a supplied
* Comparator. The array must be sorted (as by the sort() method with the
* same Comparator) - if it is not, the behaviour of this method is
* undefined, and may be an infinite loop. Further, the key must be
* comparable with every item in the array. If the array contains the key
* more than once, any one of them may be found. Note: although the
* specification allows for an infinite loop if the array is unsorted, it
* will not happen in this (JCL) implementation.
*
* @param a the array to search (must be sorted)
* @param key the value to search for
* @param c the comparator by which the array is sorted; or null to
* use the elements' natural order
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws ClassCastException if key could not be compared with one of the
* elements of a
* @throws NullPointerException if a null element is compared with natural
* ordering (only possible when c is null)
*/
public static sort(Object[], int, int)
method) - if it is not, the
* behaviour of this method is undefined, and may be an infinite loop. If
* the array contains the key more than once, any one of them may be found.
* Note: although the specification allows for an infinite loop if the array
* is unsorted, it will not happen in this implementation.
*
* @param a the array to search (must be sorted)
* @param low the lowest index to search from.
* @param hi the highest index to search to.
* @param key the value to search for
* @param c the comparator by which the array is sorted; or null to
* use the elements' natural order
* @return the index at which the key was found, or -n-1 if it was not
* found, where n is the index of the first value higher than key or
* a.length if there is no such value.
* @throws ClassCastException if key could not be compared with one of the
* elements of a
* @throws IllegalArgumentException if low > hi
* @throws ArrayIndexOutOfBoundsException if low < 0
or
* hi > a.length
.
*/
public static null
not permitted)
* @return a fixed-size list, changes to which "write through" to the array
*
* @throws NullPointerException if a
is null
.
* @see Serializable
* @see RandomAccess
* @see Arrays.ArrayList
*/
public static equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents longs in their wrapper class, Long
.
* For null
, 0 is returned.
*
* @param v an array of long numbers for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(long[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
{
int elt = (int) (v[i] ^ (v[i] >>> 32));
result = 31 * result + elt;
}
return result;
}
/**
* Returns the hashcode of an array of integer numbers. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents ints in their wrapper class, Integer
.
* For null
, 0 is returned.
*
* @param v an array of integer numbers for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(int[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + v[i];
return result;
}
/**
* Returns the hashcode of an array of short numbers. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents shorts in their wrapper class, Short
.
* For null
, 0 is returned.
*
* @param v an array of short numbers for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(short[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + v[i];
return result;
}
/**
* Returns the hashcode of an array of characters. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents chars in their wrapper class, Character
.
* For null
, 0 is returned.
*
* @param v an array of characters for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(char[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + v[i];
return result;
}
/**
* Returns the hashcode of an array of bytes. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents bytes in their wrapper class, Byte
.
* For null
, 0 is returned.
*
* @param v an array of bytes for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(byte[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + v[i];
return result;
}
/**
* Returns the hashcode of an array of booleans. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents booleans in their wrapper class,
* Boolean
. For null
, 0 is returned.
*
* @param v an array of booleans for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(boolean[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + (v[i] ? 1231 : 1237);
return result;
}
/**
* Returns the hashcode of an array of floats. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents floats in their wrapper class, Float
.
* For null
, 0 is returned.
*
* @param v an array of floats for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(float[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
result = 31 * result + Float.floatToIntBits(v[i]);
return result;
}
/**
* Returns the hashcode of an array of doubles. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object. This has the same
* data, but represents doubles in their wrapper class, Double
.
* For null
, 0 is returned.
*
* @param v an array of doubles for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(double[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
{
long l = Double.doubleToLongBits(v[i]);
int elt = (int) (l ^ (l >>> 32));
result = 31 * result + elt;
}
return result;
}
/**
* Returns the hashcode of an array of objects. If two arrays
* are equal, according to equals()
, they should have the
* same hashcode. The hashcode returned by the method is equal to that
* obtained by the corresponding List
object.
* For null
, 0 is returned.
*
* @param v an array of integer numbers for which the hash code should be
* computed.
* @return the hash code of the array, or 0 if null was given.
* @since 1.5
*/
public static int hashCode(Object[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
{
int elt = v[i] == null ? 0 : v[i].hashCode();
result = 31 * result + elt;
}
return result;
}
public static int deepHashCode(Object[] v)
{
if (v == null)
return 0;
int result = 1;
for (int i = 0; i < v.length; ++i)
{
int elt;
if (v[i] == null)
elt = 0;
else if (v[i] instanceof boolean[])
elt = hashCode((boolean[]) v[i]);
else if (v[i] instanceof byte[])
elt = hashCode((byte[]) v[i]);
else if (v[i] instanceof char[])
elt = hashCode((char[]) v[i]);
else if (v[i] instanceof short[])
elt = hashCode((short[]) v[i]);
else if (v[i] instanceof int[])
elt = hashCode((int[]) v[i]);
else if (v[i] instanceof long[])
elt = hashCode((long[]) v[i]);
else if (v[i] instanceof float[])
elt = hashCode((float[]) v[i]);
else if (v[i] instanceof double[])
elt = hashCode((double[]) v[i]);
else if (v[i] instanceof Object[])
elt = hashCode((Object[]) v[i]);
else
elt = v[i].hashCode();
result = 31 * result + elt;
}
return result;
}
/** @since 1.5 */
public static boolean deepEquals(Object[] v1, Object[] v2)
{
if (v1 == null)
return v2 == null;
if (v2 == null || v1.length != v2.length)
return false;
for (int i = 0; i < v1.length; ++i)
{
Object e1 = v1[i];
Object e2 = v2[i];
if (e1 == e2)
continue;
if (e1 == null || e2 == null)
return false;
boolean check;
if (e1 instanceof boolean[] && e2 instanceof boolean[])
check = equals((boolean[]) e1, (boolean[]) e2);
else if (e1 instanceof byte[] && e2 instanceof byte[])
check = equals((byte[]) e1, (byte[]) e2);
else if (e1 instanceof char[] && e2 instanceof char[])
check = equals((char[]) e1, (char[]) e2);
else if (e1 instanceof short[] && e2 instanceof short[])
check = equals((short[]) e1, (short[]) e2);
else if (e1 instanceof int[] && e2 instanceof int[])
check = equals((int[]) e1, (int[]) e2);
else if (e1 instanceof long[] && e2 instanceof long[])
check = equals((long[]) e1, (long[]) e2);
else if (e1 instanceof float[] && e2 instanceof float[])
check = equals((float[]) e1, (float[]) e2);
else if (e1 instanceof double[] && e2 instanceof double[])
check = equals((double[]) e1, (double[]) e2);
else if (e1 instanceof Object[] && e2 instanceof Object[])
check = equals((Object[]) e1, (Object[]) e2);
else
check = e1.equals(e2);
if (! check)
return false;
}
return true;
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(boolean[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(byte[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(char[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(short[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(int[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(long[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(float[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(double[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
/**
* Returns a String representation of the argument array. Returns "null"
* if a
is null.
* @param v the array to represent
* @return a String representing this array
* @since 1.5
*/
public static String toString(Object[] v)
{
if (v == null)
return "null";
CPStringBuilder b = new CPStringBuilder("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
b.append(v[i]);
}
b.append("]");
return b.toString();
}
private static void deepToString(Object[] v, CPStringBuilder b, HashSet seen)
{
b.append("[");
for (int i = 0; i < v.length; ++i)
{
if (i > 0)
b.append(", ");
Object elt = v[i];
if (elt == null)
b.append("null");
else if (elt instanceof boolean[])
b.append(toString((boolean[]) elt));
else if (elt instanceof byte[])
b.append(toString((byte[]) elt));
else if (elt instanceof char[])
b.append(toString((char[]) elt));
else if (elt instanceof short[])
b.append(toString((short[]) elt));
else if (elt instanceof int[])
b.append(toString((int[]) elt));
else if (elt instanceof long[])
b.append(toString((long[]) elt));
else if (elt instanceof float[])
b.append(toString((float[]) elt));
else if (elt instanceof double[])
b.append(toString((double[]) elt));
else if (elt instanceof Object[])
{
Object[] os = (Object[]) elt;
if (seen.contains(os))
b.append("[...]");
else
{
seen.add(os);
deepToString(os, b, seen);
}
}
else
b.append(elt);
}
b.append("]");
}
/** @since 1.5 */
public static String deepToString(Object[] v)
{
if (v == null)
return "null";
HashSet seen = new HashSet();
CPStringBuilder b = new CPStringBuilder();
deepToString(v, b, seen);
return b.toString();
}
/**
* Inner class used by {@link #asList(Object[])} to provide a list interface
* to an array. The name, though it clashes with java.util.ArrayList, is
* Sun's choice for Serialization purposes. Element addition and removal
* is prohibited, but values can be modified.
*
* @author Eric Blake (ebb9@email.byu.edu)
* @status updated to 1.4
*/
private static final class ArrayListfalse
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return false
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* false
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(boolean[],int,int)
*/
public static boolean[] copyOf(boolean[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with false
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with false
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(boolean[],int)
*/
public static boolean[] copyOfRange(boolean[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
boolean[] newArray = new boolean[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, false);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with (byte)0
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return (byte)0
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* (byte)0
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(byte[],int,int)
*/
public static byte[] copyOf(byte[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with (byte)0
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with (byte)0
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(byte[],int)
*/
public static byte[] copyOfRange(byte[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
byte[] newArray = new byte[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, (byte)0);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with '\0'
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return '\0'
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* '\0'
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(char[],int,int)
*/
public static char[] copyOf(char[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with '\0'
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with '\0'
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(char[],int)
*/
public static char[] copyOfRange(char[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
char[] newArray = new char[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, '\0');
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with 0d
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return 0d
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* 0d
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(double[],int,int)
*/
public static double[] copyOf(double[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with 0d
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with 0d
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(double[],int)
*/
public static double[] copyOfRange(double[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
double[] newArray = new double[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, 0d);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with 0f
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return 0f
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* 0f
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(float[],int,int)
*/
public static float[] copyOf(float[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with 0f
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with 0f
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(float[],int)
*/
public static float[] copyOfRange(float[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
float[] newArray = new float[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, 0f);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with 0
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return 0
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* 0
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(int[],int,int)
*/
public static int[] copyOf(int[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with 0
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with 0
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(int[],int)
*/
public static int[] copyOfRange(int[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
int[] newArray = new int[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, 0);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with 0L
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return 0L
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* 0L
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(long[],int,int)
*/
public static long[] copyOf(long[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with 0L
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with 0L
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(long[],int)
*/
public static long[] copyOfRange(long[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
long[] newArray = new long[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, 0L);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with (short)0
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return (short)0
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* (short)0
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(short[],int,int)
*/
public static short[] copyOf(short[] original, int newLength)
{
if (newLength < 0)
throw new NegativeArraySizeException("The array size is negative.");
return copyOfRange(original, 0, newLength);
}
/**
* Copies the specified range of the supplied array to a new
* array, padding as necessary with (short)0
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with (short)0
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(short[],int)
*/
public static short[] copyOfRange(short[] original, int from, int to)
{
if (from > to)
throw new IllegalArgumentException("The initial index is after " +
"the final index.");
short[] newArray = new short[to - from];
if (to > original.length)
{
System.arraycopy(original, from, newArray, 0,
original.length - from);
fill(newArray, original.length, newArray.length, (short)0);
}
else
System.arraycopy(original, from, newArray, 0, to - from);
return newArray;
}
/**
* Returns a copy of the supplied array, truncating or padding as
* necessary with null
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return null
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength)
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @return a copy of the original array, truncated or padded with
* null
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(T[],int,int)
*/
public static null
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with null
will be
* returned). The returned array is always of length
* to - from
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(T[],int)
*/
public static null
to obtain the specified length.
* Indices that are valid for both arrays will return the same value.
* Indices that only exist in the returned array (due to the new length
* being greater than the original length) will return null
.
* This is equivalent to calling
* copyOfRange(original, 0, newLength, newType)
. The returned
* array will be of the specified type, newType
.
*
* @param original the original array to be copied.
* @param newLength the length of the returned array.
* @param newType the type of the returned array.
* @return a copy of the original array, truncated or padded with
* null
to obtain the required length.
* @throws NegativeArraySizeException if newLength
is negative.
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOfRange(U[],int,int,Class)
*/
public static null
* if to
is greater than the length of the original
* array. from
must be in the range zero to
* original.length
and can not be greater than
* to
. The initial element of the
* returned array will be equal to original[from]
,
* except where from
is equal to to
* (where a zero-length array will be returned) or
*
from
is equal to original.length
* (where an array padded with null
will be
* returned). The returned array is always of length
* to - from
and will be of the specified type,
* newType
.
*
* @param original the array from which to copy.
* @param from the initial index of the range, inclusive.
* @param to the final index of the range, exclusive.
* @param newType the type of the returned array.
* @return a copy of the specified range, with padding to
* obtain the required length.
* @throws ArrayIndexOutOfBoundsException if from < 0
* or from > original.length
* @throws IllegalArgumentException if from > to
* @throws NullPointerException if original
is null
.
* @since 1.6
* @see #copyOf(T[],int)
*/
public static