ch.javasoft.util
Class Arrays

java.lang.Object
  extended by ch.javasoft.util.Arrays

public class Arrays
extends Object

The Arrays class contains static helper methods arround arrays, similar to Arrays, but adding missing stuff.


Field Summary
static double[] EMPTY_DOUBLE
           
static float[] EMPTY_FLOAT
           
static int[] EMPTY_INT
           
static long[] EMPTY_LONG
           
static Object[] EMPTY_OBJECT
           
 
Method Summary
static
<T> Set<T>
asSet(T... a)
          Returns a modifiable LinkedHashSet containing the submitted elements.
static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
          Searches a range of the specified array of ints for the specified value using the binary search algorithm.
static int[] copyOf(int[] original, int newLength)
          Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static
<T> T[]
copyOf(T[] original, int newLength)
          Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
static
<T,U> T[]
copyOf(U[] original, int newLength, Class<? extends T[]> newType)
          Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
static double[] copyOfRange(double[] original, int from, int to)
          Copies the specified range of the specified array into a new array.
static int[] copyOfRange(int[] original, int from, int to)
          Copies the specified range of the specified array into a new array.
static long[] copyOfRange(long[] original, int from, int to)
          Copies the specified range of the specified array into a new array.
static
<T> T[]
copyOfRange(T[] original, int from, int to)
          Copies the specified range of the specified array into a new array.
static
<T,U> T[]
copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
          Copies the specified range of the specified array into a new array.
static boolean[] merge(boolean[] arr, boolean... others)
           
static byte[] merge(byte[] arr, byte... others)
           
static char[] merge(char[] arr, char... others)
           
static double[] merge(double[] arr, double... others)
           
static float[] merge(float[] arr, float... others)
           
static int[] merge(int[] arr, int... others)
           
static long[] merge(long[] arr, long... others)
           
static short[] merge(short[] arr, short... others)
           
static
<T> T[]
merge(T[] arr, T... others)
           
static
<T> T[]
newArray(Class<T> clazz, int length)
           
static
<T> T[]
newArray(T[] template, int length)
           
static void printArray(double[] arr)
           
static void printArray(double[][] arr)
           
static void printArray(int[] arr)
           
static void printArray(int[][] arr)
           
static void printArray(PrintStream out, double[] arr)
           
static void printArray(PrintStream out, double[][] arr)
           
static void printArray(PrintStream out, int[] arr)
           
static void printArray(PrintStream out, int[][] arr)
           
static void swap(boolean[] arr, int indexA, int indexB)
           
static void swap(byte[] arr, int indexA, int indexB)
           
static void swap(char[] arr, int indexA, int indexB)
           
static void swap(double[] arr, int indexA, int indexB)
           
static void swap(float[] arr, int indexA, int indexB)
           
static void swap(int[] arr, int indexA, int indexB)
           
static void swap(long[] arr, int indexA, int indexB)
           
static void swap(Object[] arr, int indexA, int indexB)
           
static void swap(short[] arr, int indexA, int indexB)
           
static void swapCol(boolean[][] arr, int colA, int colB)
           
static void swapCol(byte[][] arr, int colA, int colB)
           
static void swapCol(char[][] arr, int colA, int colB)
           
static void swapCol(double[][] arr, int colA, int colB)
           
static void swapCol(float[][] arr, int colA, int colB)
           
static void swapCol(int[][] arr, int colA, int colB)
           
static void swapCol(long[][] arr, int colA, int colB)
           
static void swapCol(Object[][] arr, int colA, int colB)
           
static void swapCol(short[][] arr, int colA, int colB)
           
static void swapRow(boolean[][] arr, int rowA, int rowB)
           
static void swapRow(byte[][] arr, int rowA, int rowB)
           
static void swapRow(char[][] arr, int rowA, int rowB)
           
static void swapRow(double[][] arr, int rowA, int rowB)
           
static void swapRow(float[][] arr, int rowA, int rowB)
           
static void swapRow(int[][] arr, int rowA, int rowB)
           
static void swapRow(long[][] arr, int rowA, int rowB)
           
static void swapRow(Object[][] arr, int rowA, int rowB)
           
static void swapRow(short[][] arr, int rowA, int rowB)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_OBJECT

public static Object[] EMPTY_OBJECT

EMPTY_LONG

public static long[] EMPTY_LONG

EMPTY_INT

public static int[] EMPTY_INT

EMPTY_DOUBLE

public static double[] EMPTY_DOUBLE

EMPTY_FLOAT

public static float[] EMPTY_FLOAT
Method Detail

asSet

public static <T> Set<T> asSet(T... a)
Returns a modifiable LinkedHashSet containing the submitted elements. If the list is empty or null, an empty linked hash set is returned. In any case, the returned set is modifiable.

Type Parameters:
T - the element type
Parameters:
a - the element(s)
Returns:
a linked hash set containing the elements

swap

public static void swap(Object[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(long[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(int[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(short[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(byte[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(char[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(boolean[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(double[] arr,
                        int indexA,
                        int indexB)

swap

public static void swap(float[] arr,
                        int indexA,
                        int indexB)

swapRow

public static void swapRow(Object[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(long[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(int[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(short[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(byte[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(char[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(boolean[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(double[][] arr,
                           int rowA,
                           int rowB)

swapRow

public static void swapRow(float[][] arr,
                           int rowA,
                           int rowB)

swapCol

public static void swapCol(Object[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(long[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(int[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(short[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(byte[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(char[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(boolean[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(double[][] arr,
                           int colA,
                           int colB)

swapCol

public static void swapCol(float[][] arr,
                           int colA,
                           int colB)

newArray

public static <T> T[] newArray(T[] template,
                               int length)

newArray

public static <T> T[] newArray(Class<T> clazz,
                               int length)

merge

public static <T> T[] merge(T[] arr,
                            T... others)

merge

public static long[] merge(long[] arr,
                           long... others)

merge

public static int[] merge(int[] arr,
                          int... others)

merge

public static short[] merge(short[] arr,
                            short... others)

merge

public static byte[] merge(byte[] arr,
                           byte... others)

merge

public static char[] merge(char[] arr,
                           char... others)

merge

public static boolean[] merge(boolean[] arr,
                              boolean... others)

merge

public static double[] merge(double[] arr,
                             double... others)

merge

public static float[] merge(float[] arr,
                            float... others)

printArray

public static void printArray(int[] arr)

printArray

public static void printArray(double[] arr)

printArray

public static void printArray(PrintStream out,
                              int[] arr)

printArray

public static void printArray(PrintStream out,
                              double[] arr)

printArray

public static void printArray(int[][] arr)

printArray

public static void printArray(double[][] arr)

printArray

public static void printArray(PrintStream out,
                              int[][] arr)

printArray

public static void printArray(PrintStream out,
                              double[][] arr)

copyOfRange

public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

The resulting array is of exactly the same class as the original array.

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
Returns:
a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length
Throws:
ArrayIndexOutOfBoundsException - if from < 0 or from > original.length
IllegalArgumentException - if from > to
NullPointerException - if original is null
Since:
1.6

copyOfRange

public static <T,U> T[] copyOfRange(U[] original,
                                    int from,
                                    int to,
                                    Class<? extends T[]> newType)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from. The resulting array is of the class newType.

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
newType - the class of the copy to be returned
Returns:
a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length
Throws:
ArrayIndexOutOfBoundsException - if from < 0 or from > original.length
IllegalArgumentException - if from > to
NullPointerException - if original is null
ArrayStoreException - if an element copied from original is not of a runtime type that can be stored in an array of class newType.
Since:
1.6

copyOfRange

public static double[] copyOfRange(double[] original,
                                   int from,
                                   int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case 0d is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
Returns:
a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length
Throws:
ArrayIndexOutOfBoundsException - if from < 0 or from > original.length
IllegalArgumentException - if from > to
NullPointerException - if original is null
Since:
1.6

copyOfRange

public static long[] copyOfRange(long[] original,
                                 int from,
                                 int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case 0d is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
Returns:
a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length
Throws:
ArrayIndexOutOfBoundsException - if from < 0 or from > original.length
IllegalArgumentException - if from > to
NullPointerException - if original is null
Since:
1.6

copyOfRange

public static int[] copyOfRange(int[] original,
                                int from,
                                int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case 0 is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.

Parameters:
original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
Returns:
a new array containing the specified range from the original array, truncated or padded with zeros to obtain the required length
Throws:
ArrayIndexOutOfBoundsException - if from < 0 or from > original.length()
IllegalArgumentException - if from > to
NullPointerException - if original is null
Since:
1.6

copyOf

public static <T> T[] copyOf(T[] original,
                             int newLength)
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.

Parameters:
original - the array to be copied
newLength - the length of the copy to be returned
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length
Throws:
NegativeArraySizeException - if newLength is negative
NullPointerException - if original is null
Since:
1.6

copyOf

public static <T,U> T[] copyOf(U[] original,
                               int newLength,
                               Class<? extends T[]> newType)
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of the class newType.

Parameters:
original - the array to be copied
newLength - the length of the copy to be returned
newType - the class of the copy to be returned
Returns:
a copy of the original array, truncated or padded with nulls to obtain the specified length
Throws:
NegativeArraySizeException - if newLength is negative
NullPointerException - if original is null
ArrayStoreException - if an element copied from original is not of a runtime type that can be stored in an array of class newType
Since:
1.6

copyOf

public static int[] copyOf(int[] original,
                           int newLength)
Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain 0. Such indices will exist if and only if the specified length is greater than that of the original array.

Parameters:
original - the array to be copied
newLength - the length of the copy to be returned
Returns:
a copy of the original array, truncated or padded with zeros to obtain the specified length
Throws:
NegativeArraySizeException - if newLength is negative
NullPointerException - if original is null
Since:
1.6

binarySearch

public static int binarySearch(int[] a,
                               int fromIndex,
                               int toIndex,
                               int key)
Searches a range of the specified array of ints for the specified value using the binary search algorithm. The range must be sorted (as by the Arrays.sort(int[], int, int) method) prior to making this call. If it is not sorted, the results are undefined. If the range contains multiple elements with the specified value, there is no guarantee which one will be found.

Parameters:
a - the array to be searched
fromIndex - the index of the first element (inclusive) to be searched
toIndex - the index of the last element (exclusive) to be searched
key - the value to be searched for
Returns:
index of the search key, if it is contained in the array within the specified range; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element in the range greater than the key, or toIndex if all elements in the range are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
Throws:
IllegalArgumentException - if fromIndex > toIndex
ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > a.length
Since:
1.6