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:
3.6
Author:
Bob Carpenter

Field Summary
static char[] EMPTY_CHAR_ARRAY
          A length 0 array of characters.
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 arrayToCSV(Object[] xs)
          Converts an array of objects to comma-separated values notation.
static String arrayToCSV(String[][] elts)
          Converts the two-dimensional array of strings to comma-separated value notaiton.
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 arrayToStringBuffer(StringBuffer 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 String[] csvToArray(String csvs)
          Converts a string of comma-separated values into an array of strings.
static String[][] csvToArray2D(String csvs)
          Converts a comma-separated values string to a two-dimensional array of strings.
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 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 int sum(int[] xs)
          Returns the sum of the specified integer array.
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.


EMPTY_CHAR_ARRAY

public static final char[] EMPTY_CHAR_ARRAY
A length 0 array of characters.

Method Detail

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 and the remaining elements are set to zero.

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.

csvToArray

public static String[] csvToArray(String csvs)
Converts a string of comma-separated values into an array of strings. The returned array will be at least one element long, with values being the maximal-length strings between commas. Values may be of length zero.

Any comma (,), backslash (\) or newline (\n) that appears in a value is escaped with a backslash (\). No other use of backslash or comma in values is permitted. An attempt to decode an ill-formed input will throw an exception.

Some examples of strings and arrays they return, using Java's string-literal escapes, are:

CSV String
"" { "" }
"a" { "a" }
"a,b" { "a", "b" }
"abc,def,g" { "abc", "def", "g" }
"," { "", "" }
",,,b" { "", "", "", "b" }
"a\,b" { "a,b" }
"\\\\" { "\\" }
"ab\\\\" { "ab\\" }
"\\\n" { "\n" }
"a\\,bc,d\\,e,,f" { "a,bc", "d\", "e", "", "f" }
"ab\\" IllegalArgumentException
"a\\bc" IllegalArgumentException
"\" IllegalArgumentException
"\\\" IllegalArgumentException

Given the use of java escapes, "ab\\" is the three character sequence a, b and backslash, with the backslash escaped as \\. Similarly, "\\\n" is the two character string composed of a backslash followed by a newline character.

Parameters:
csvs - Comma-separated values to separate.
Returns:
Array of values.
Throws:
IllegalArgumentException - if the input is not well formed.

csvToArray2D

public static String[][] csvToArray2D(String csvs)
Converts a comma-separated values string to a two-dimensional array of strings. Newlines (\n<\code>) are used to separate rows, and each row is in CSV notation, as defined in csvToArray(String) for more information on the row encoding.

Here are some examples:

CSV Array
"" { { "" } }
"a" { { "a" } }
"a,b" { { "a", "b" } }
"\n" { { "" }, { "" } }
"\n\n" { { "" }, { "" }, { "" } }
"\\\n" { { "\n" } }

Any illegal argument for csvToArray(String) is also an illegal argument for this method.

Parameters:
csvs - Input comma-separated values.
Returns:
Array of arrays of strings derived from CSVs.
Throws:
IllegalArgumentException - If the CVSs are not well formed.

arrayToCSV

public static String arrayToCSV(String[][] elts)
Converts the two-dimensional array of strings to comma-separated value notaiton. See csvToArray2D(String) for more information on the encoding. The input may be a ragged array.

Parameters:
elts - Array of arrays to encode.
Returns:
Comma-separated values representation of the array.

arrayToCSV

public static String arrayToCSV(Object[] xs)
Converts an array of objects to comma-separated values notation. Each object will be converted to a string using its toString() method.

See csvToArray(String) for information on the encoding and the reverse mapping.

Parameters:
xs - Array of strings to encode.
Returns:
CSV-encoded array of strings.

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.

arrayToStringBuffer

public static void arrayToStringBuffer(StringBuffer 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.

sum

public static int sum(int[] xs)
Returns the sum of the specified integer array. Note that there is no check for overflow.

Parameters:
xs - Array of integers to sum.
Returns:
Sum of the array.

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.

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.

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.