com.aliasi.util
Class Arrays

java.lang.Object
  extended by com.aliasi.util.Arrays

public class Arrays
extends Object

Static utility methods for processing arrays.

Since:
LingPipe1.0
Version:
4.0.0
Author:
Bob Carpenter

Field Summary
static int[] EMPTY_INT_ARRAY
          A length 0 array of integers.
 
Method Summary
static char[] add(char c, char[] cs)
          Return the result of adding the specified character to the specified sorted character array.
static String arrayToString(Object[] xs)
          Returns the concatenation of the string representations of the specified objects separated by commas, with the whole surrounded by square brackets and separated by a comma.
static void arrayToStringBuilder(StringBuilder sb, Object[] xs)
          Appends to the string buffer the concatenation of the string representations of the specified objects separated by commas, with the whole surrounded by square brackets and separated by a comma.
static char[] concatenate(char[] cs, char c)
          Returns the array of characters consisting of the members of the first specified array followed by the specified character.
static String[] concatenate(String[] xs, String[] ys)
          Returns a new array of strings containing the elements of the first array of strings specified followed by the elements of the second array of strings specified.
static char[] copy(char[] cs)
          Return a shallow copy of the specified array that contains the same elements as the specified array.
static boolean equals(Object[] xs, Object[] ys)
          Return true if the specified arrays are the same length and contain the same elements.
static boolean member(char c, char[] cs)
          Returns true if the specified character is a member of the specified array.
static boolean member(Object x, Object[] xs)
          Returns true if the specified object is an element of the specified array.
static
<E> void
permute(E[] xs)
          Randomly permutes the elements of the specified array using a freshly generated randomizer.
static
<E> void
permute(E[] xs, Random random)
          Randomly permutes the elements of the specified array using the specified randomizer.
static void permute(int[] xs)
          Randomly permutes the elements of the specified integer array using a newly created randomizer.
static void permute(int[] xs, Random random)
          Randomly permutes the elements of the specified integer array using the specified randomizer.
static
<E> E[]
reallocate(E[] xs, int newSize)
          Returns a copy of the specified array of objects of the specified size.
static int[] reallocate(int[] xs)
          Reallocates the specified integer array to be 50 percent longer, with a minimum growth in length of one element.
static int[] reallocate(int[] xs, int newSize)
          Returns a copy of the specified array of integers of the specified size.
static char[] toArray(CharSequence cSeq)
          Converts the specified character sequence to an array of characters.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_INT_ARRAY

public static final int[] EMPTY_INT_ARRAY
A length 0 array of integers.

Method Detail

reallocate

public static <E> E[] reallocate(E[] xs,
                                 int newSize)
Returns a copy of the specified array of objects of the specified size. The runtime type of the array returned will be that of the specified input array.

As many of the original elements as will fit in the new array are copied into the returned array. If the new size is longer, the remaining elements will be null.

Type Parameters:
E - type of objects in array.
Parameters:
xs - Original array.
newSize - Size of returned array.
Returns:
New array of specified length with as many elements copied from the original array as will fit.

reallocate

public static int[] reallocate(int[] xs,
                               int newSize)
Returns a copy of the specified array of integers of the specified size. As many of the original elements as will fit in the new array are copied. If the new size is longer, the remaining elements will be 0.

Parameters:
xs - Original array.
newSize - Length of returned array.
Returns:
New array of specified length with as many elements copied from the original array as will fit.

reallocate

public static int[] reallocate(int[] xs)
Reallocates the specified integer array to be 50 percent longer, with a minimum growth in length of one element. All of the elements of the specified array will be copied into the resulting array and the remaining elements initialized to zero (0).

Parameters:
xs - Array to reallocate.
Returns:
Result of reallocation.

add

public static char[] add(char c,
                         char[] cs)
Return the result of adding the specified character to the specified sorted character array. The original array will be returned if the character is in the array, otherwise a new array will be constructed and returned.

Warning: No check is done that the incoming character array is in order.

Parameters:
c - Character to add.
cs - Array of characters in sorted order.
Returns:
The result of adding the character to the array in the correct order, returning a larger array if necessary.

copy

public static char[] copy(char[] cs)
Return a shallow copy of the specified array that contains the same elements as the specified array. If the input is null, then null is returned.

Parameters:
cs - Array to copy.
Returns:
Shallow copy of array.

toArray

public static char[] toArray(CharSequence cSeq)
Converts the specified character sequence to an array of characters.

Parameters:
cSeq - Character sequence to convert.
Returns:
Array of characters in sequence.

member

public static boolean member(Object x,
                             Object[] xs)
Returns true if the specified object is an element of the specified array. Returns false if the specified array is null.

Parameters:
x - Object to test for membership.
xs - Array to test for object.
Returns:
true if the specified object is an element of the specified array.

member

public static boolean member(char c,
                             char[] cs)
Returns true if the specified character is a member of the specified array. Returns false if the specified array is null.

Parameters:
c - Character to test for membership.
cs - Array to test for character.
Returns:
true if the specified character is an element of the specified array.

arrayToString

public static String arrayToString(Object[] xs)
Returns the concatenation of the string representations of the specified objects separated by commas, with the whole surrounded by square brackets and separated by a comma.

Parameters:
xs - Array of which to return a string representation.
Returns:
String representation of the specified array.

arrayToStringBuilder

public static void arrayToStringBuilder(StringBuilder sb,
                                        Object[] xs)
Appends to the string buffer the concatenation of the string representations of the specified objects separated by commas, with the whole surrounded by square brackets and separated by a comma.

Parameters:
sb - String buffer to which string representation is appended.
xs - Array of which to return a string representation.

concatenate

public static char[] concatenate(char[] cs,
                                 char c)
Returns the array of characters consisting of the members of the first specified array followed by the specified character.

Parameters:
cs - Characters to start resulting array.
c - Last character in resulting array.
Returns:
Array of characters consisting of the characters in the first array followed by the last character.
Throws:
NullPointerException - If the array of characters is null.

concatenate

public static String[] concatenate(String[] xs,
                                   String[] ys)
Returns a new array of strings containing the elements of the first array of strings specified followed by the elements of the second array of strings specified.

Parameters:
xs - First array of strings.
ys - Second array of strings.
Returns:
Concatenation of first array of strings followed by the second array of strings.

equals

public static boolean equals(Object[] xs,
                             Object[] ys)
Return true if the specified arrays are the same length and contain the same elements.

Parameters:
xs - First array.
ys - Second array.
Returns:
true if the specified arrays are the same length and contain the same elements.

permute

public static <E> void permute(E[] xs)
Randomly permutes the elements of the specified array using a freshly generated randomizer. The resulting array will have the same elements, but arranged into a (possibly) different order.

Type Parameters:
E - the type of objects in the array being permuted
Parameters:
xs - Array to permute.

permute

public static <E> void permute(E[] xs,
                               Random random)
Randomly permutes the elements of the specified array using the specified randomizer. The resulting array will have the same elements, but arranged into a (possibly) different order.

Type Parameters:
E - the type of objects in the array being permuted
Parameters:
xs - Array to permute.
random - Randomizer to use for permuation.

permute

public static void permute(int[] xs)
Randomly permutes the elements of the specified integer array using a newly created randomizer. The resulting array will have the same elements, but arranged into a (possibly) different order. The randomizer is created with a call to the nullary constructor Random.Random().

Parameters:
xs - Array to permute.

permute

public static void permute(int[] xs,
                           Random random)
Randomly permutes the elements of the specified integer array using the specified randomizer.

Parameters:
xs - Array to permute.
random - Randomizer to use for permutations.