com.jstatcom.util
Class UStringArray

java.lang.Object
  extended by com.jstatcom.util.UStringArray

public final class UStringArray
extends java.lang.Object

A collection of static methods that are related to arrays of strings.

All methods implemented do not modify the original input. Results are always newly created arrays. If the input is somehow wrong an appropriate exception is thrown. All methods require that the input arrays have the same number of elements in each row.

Author:
Markus Kraetzig

Method Summary
static java.lang.String[][] appendStringCols(java.lang.String[][] orig, java.lang.String[][] toAppend)
          Appends toAppend to the columns of orig and returns the new merged array.
static java.lang.String[][] appendStringRows(java.lang.String[][] orig, java.lang.String[][] toAppend)
          Appends toAppend to the rows of orig and returns the new merged array.
static java.lang.String[][] appendSuffix(java.lang.String[][] orig, java.lang.String suffix)
          Appends suffix to the elements of orig and returns the new string array.
static java.lang.String[] appendSuffix(java.lang.String[] orig, java.lang.String suffix)
          Appends suffix to the elements of orig and returns the new string array.
static void checkRowLengths(java.lang.String[][] arg)
          Checks whether all rows of arg have the same number of columns.
static java.lang.String[][] cloneStringArray(java.lang.String[][] x)
          Gets an identical copy of x.
static boolean compareStringArrays(java.lang.String[][] arg1, java.lang.String[][] arg2)
          Compares two string arrays and returns true. if the dimensions are the same and all elements are equal.
static java.lang.String[] createNamedIndex(java.lang.String name, double[] idx)
          Creates an index with its elements starting with name followed by the integer part of the respective element of idx.
static java.lang.String[][] delCol(java.lang.String[][] arg, int colIndex)
          Deletes the column colIndex from arg.
static java.lang.String[][] delif(java.lang.String[][] arg, int[] index)
          Deletes columns of arg that have been selected by index and returns a new array with the remaining columns.
static java.lang.String[][] delRow(java.lang.String[][] arg, int rowIndex)
          Deletes the row rowIndex from arg.
static java.lang.String[][] delRowsIf(java.lang.String[][] arg, int[] index)
          Deletes rows of arg that have been selected by index and returns a new array with the remaining rows.
static java.lang.String[] delRowsIf(java.lang.String[] arg, int[] rowIndex)
          Deletes rows of arg that have been selected by index and returns a new array with the remaining rows.
static java.lang.String[] getStringCol(java.lang.String[][] arg, int colIndex)
          Gets the column of the array arg specified with colIndex.
static java.lang.String[] getStringDiff(java.lang.String[] arg1, java.lang.String[] arg2)
          Gets a new string array with only the rows of arg1 that do not appear in arg2.
static java.lang.String[][] getStringRows(java.lang.String[][] arg, int startRow, int endRow)
          Gets all rows of the array arg defined by startRow:endRow.
static java.lang.String[][] selif(java.lang.String[][] arg, int[] index)
          Gets all columns of the array arg that have a nonzero corresponding element in index.
static java.lang.String[][] selRowsIf(java.lang.String[][] arg, int[] index)
          Gets all rows of the array arg that have a nonzero corresponding element in index.
static java.lang.String[] selRowsIf(java.lang.String[] arg, int[] rowIndex)
          Gets a new string array with all elements arg that have a corresponding value in rowIndex that is !
static java.lang.String[][] toStringMatrix(java.lang.String[] arg)
          Transforms a one-dimensional array into a K x 1 pseudo-two-dimensional array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

appendStringCols

public static java.lang.String[][] appendStringCols(java.lang.String[][] orig,
                                                    java.lang.String[][] toAppend)
Appends toAppend to the columns of orig and returns the new merged array. This only works, if orig and toAppend have the same number of rows.

Parameters:
orig - the array to append the columns of toAppend to
toAppend - the arrays to be appended to orig
Returns:
a new array orig~toAppend
Throws:
java.lang.IllegalArgumentException - if (orig.length != toAppend.length) or if (orig == null || toAppend == null) or if rows of an argument have different lengths

appendStringRows

public static java.lang.String[][] appendStringRows(java.lang.String[][] orig,
                                                    java.lang.String[][] toAppend)
Appends toAppend to the rows of orig and returns the new merged array. This only works, if orig and toAppend have the same number of columns.

Parameters:
orig - the array to append the rows of toAppend to
toAppend - the arrays to be appended to orig
Returns:
a new array orig|toAppend
Throws:
java.lang.IllegalArgumentException - if (orig[0].length != toAppend[0].length) or if (orig == null || toAppend == null) or if rows of an argument have different lengths

appendSuffix

public static java.lang.String[][] appendSuffix(java.lang.String[][] orig,
                                                java.lang.String suffix)
Appends suffix to the elements of orig and returns the new string array.

Parameters:
orig - the original string array
suffix - the suffix to be appended to all elements of orig
Returns:
a new string array
Throws:
java.lang.IllegalArgumentException - if (orig == null || suffix == null) or if rows of argument have different lengths

appendSuffix

public static java.lang.String[] appendSuffix(java.lang.String[] orig,
                                              java.lang.String suffix)
Appends suffix to the elements of orig and returns the new string array.

Parameters:
orig - the original string array
suffix - the suffix to be appended to all elements of orig
Returns:
a new string array
Throws:
java.lang.IllegalArgumentException - if (orig == null || suffix == null)

checkRowLengths

public static void checkRowLengths(java.lang.String[][] arg)
Checks whether all rows of arg have the same number of columns. If the check fails, an exception is thrown.

Parameters:
arg - the String[][] to check
Throws:
java.lang.IllegalArgumentException - if the rows of arg have different lengths

cloneStringArray

public static java.lang.String[][] cloneStringArray(java.lang.String[][] x)
Gets an identical copy of x.

Returns:
the cloned array or
  • null if (x == null)
  • new String[0][0] if (x.length == 0 || x[0].length == 0)
Throws:
java.lang.IllegalArgumentException - if rows of argument have different lengths

compareStringArrays

public static boolean compareStringArrays(java.lang.String[][] arg1,
                                          java.lang.String[][] arg2)
Compares two string arrays and returns true. if the dimensions are the same and all elements are equal. If two elements of arg1 and arg2 are null, they are considered to be equal. The comparison is case sensitive.

Parameters:
arg1 - string array to compare
arg2 - string array to compare
Returns:
true if dimensions and all elements are equal or if arg1, arg2 have both at least one dimension of zero,
false otherwise
Throws:
java.lang.IllegalArgumentException - if rows of an argument have different lengths

createNamedIndex

public static java.lang.String[] createNamedIndex(java.lang.String name,
                                                  double[] idx)
Creates an index with its elements starting with name followed by the integer part of the respective element of idx. The resulting string array has length idx.length.

Parameters:
name - the string with the prefix for the index
idx - the array containing the indices to be appended to name
Returns:
a idx.length string array
Throws:
java.lang.IllegalArgumentException - if (name == null || idx == null)

delCol

public static java.lang.String[][] delCol(java.lang.String[][] arg,
                                          int colIndex)
Deletes the column colIndex from arg.

Parameters:
arg - the string array to delete the column from
colIndex - index of the column
Returns:
new string array with the deleted column
Throws:
java.lang.IllegalArgumentException - if (arg == null) or if (colIndex < 0 || arg[0].length < colIndex) or if (arg.length == 0 || arg[0].length == 0) or if rows of argument have different lengths

delif

public static java.lang.String[][] delif(java.lang.String[][] arg,
                                         int[] index)
Deletes columns of arg that have been selected by index and returns a new array with the remaining columns. A column is selected for deletion if the corresponding index entry is nonzero.

Parameters:
arg - the array to delete columns from
index - the arg[0].length x 1 int array
Returns:
a new array with the selected columns deleted or new String[0][0] if all columns where selected for deletion or if arg.length == 0
Throws:
java.lang.IllegalArgumentException - if (arg == null || index == null) or if (arg[0].length != index.length) or if ((arg.length == 0 || arg[0].length == 0) && index.length > 0) or if rows of argument have different lengths

delRow

public static java.lang.String[][] delRow(java.lang.String[][] arg,
                                          int rowIndex)
Deletes the row rowIndex from arg.

Parameters:
arg - the string array to delete the row from
rowIndex - index of the row
Returns:
new string array with the deleted row
Throws:
java.lang.IllegalArgumentException - if (arg == null) or if (rowIndex < 0 || arg.length < rowIndex) or if (arg.length == 0 || arg[0].length == 0) or if rows of argument have different lengths

delRowsIf

public static java.lang.String[][] delRowsIf(java.lang.String[][] arg,
                                             int[] index)
Deletes rows of arg that have been selected by index and returns a new array with the remaining rows. A row is selected for deletion if the corresponding index entry is nonzero.

Parameters:
arg - the array to delete rows from
index - the arg[0].length x 1 int array
Returns:
a new array with the selected rows deleted or new String[0][0] if all rows where selected for deletion or if arg.length == 0
Throws:
java.lang.IllegalArgumentException - if (arg == null || index == null) or if (arg.length != index.length) or if ((arg.length == 0 || arg[0].length == 0) && index.length > 0) or if rows of arg have different lengths

delRowsIf

public static java.lang.String[] delRowsIf(java.lang.String[] arg,
                                           int[] rowIndex)
Deletes rows of arg that have been selected by index and returns a new array with the remaining rows. A row is selected for deletion if the corresponding index entry is set to 1 (or != 0).

Parameters:
arg - the array to delete rows from
rowIndex - the arg.length x 1 int array with 1's and 0's (all values different from 0 select a row, but usually 1)
Returns:
a new array with the selected rows deleted or new String[0][0] if all rows where selected for deletion or if arg.length == 0
Throws:
java.lang.IllegalArgumentException - if (arg == null || index == null) or if (arg.length != index.length)

getStringCol

public static java.lang.String[] getStringCol(java.lang.String[][] arg,
                                              int colIndex)
Gets the column of the array arg specified with colIndex. The index starts with 0 as usual.

Parameters:
arg - the array to extract a column from
colIndex - the index of the column to extract (starting from 0)
Returns:
a one dimensional string array with the column data
Throws:
java.lang.IllegalArgumentException - if (arg == null) or if (arg.length == 0) or if the index is outside the valid array bounds or if rows of argument have different lengths

getStringDiff

public static java.lang.String[] getStringDiff(java.lang.String[] arg1,
                                               java.lang.String[] arg2)
Gets a new string array with only the rows of arg1 that do not appear in arg2.

Parameters:
arg1 - the string array to delete rows from that also appear in arg2
arg2 - contains strings that are deleted from arg1
Returns:
a new string array object with deleted rows
Throws:
java.lang.IllegalArgumentException - if (arg1 == null || arg2 == null)

getStringRows

public static java.lang.String[][] getStringRows(java.lang.String[][] arg,
                                                 int startRow,
                                                 int endRow)
Gets all rows of the array arg defined by startRow:endRow. The indices start with 0 as usual.

Parameters:
arg - the array to extract rows from
startRow - the index of the first row to extract (starting from 0)
endRow - the index of the last row to extract (starting from 0)
Returns:
a one dimensional string array with the row data
Throws:
java.lang.IllegalArgumentException - if (arg == null) or if (arg.length == 0) or if (startRow > endRow) or if the indices are outside the valid array bounds or if rows of argument have different lengths

selif

public static java.lang.String[][] selif(java.lang.String[][] arg,
                                         int[] index)
Gets all columns of the array arg that have a nonzero corresponding element in index.

Parameters:
arg - the array to extract columns from
index - arg[0].length x 1 vector selecting columns from arg
Returns:
new array with the extracted columns, new String[0][0] if index contains only 0's, a copy of arg if index contains only nonzero elements
Throws:
java.lang.IllegalArgumentException - if (arg == null || index == null) or if ((arg.length == 0 || arg[0].length == 0) && index.length > 0) or if (arg[0].length != index.length) or if rows of argument have different lengths

selRowsIf

public static java.lang.String[][] selRowsIf(java.lang.String[][] arg,
                                             int[] index)
Gets all rows of the array arg that have a nonzero corresponding element in index.

Parameters:
arg - the array to extract rows from
index - arg[0].length x 1 vector selecting rows from arg
Returns:
new array with the extracted rows, new String[0][0] if index contains only 0's, a copy of arg if index contains only nonzero elements
Throws:
java.lang.IllegalArgumentException - if (arg == null || index == null) or if (arg.length == 0 || arg[0].length == 0) && index.length > 0) or if (arg.length != index.length) or if rows of argument have different lengths

selRowsIf

public static java.lang.String[] selRowsIf(java.lang.String[] arg,
                                           int[] rowIndex)
Gets a new string array with all elements arg that have a corresponding value in rowIndex that is != 0. arg and rowIndex must have the same dimension.

Parameters:
arg - the array to extract elements from
rowIndex - the index selecting elements from arg if rowIndex[i] != 0
Returns:
a one dimensional string array with the selected elements
Throws:
java.lang.IllegalArgumentException - if (arg == null || rowIndex == null) or if (arg.length != rowIndex.length)

toStringMatrix

public static java.lang.String[][] toStringMatrix(java.lang.String[] arg)
Transforms a one-dimensional array into a K x 1 pseudo-two-dimensional array. This is sometimes needed to prepare arguments for methods that take String[][] instead of String[].

Parameters:
arg - the vector-like array to transform
Returns:
two-dimensional array with dimensions arg.length x 1 or
  • null if (arg == null)
  • new String[0][0] if (arg.length == 0)