Methods Summary |
---|
public static boolean | contains(int[] array, int elem)Returns true if the given {@code array} contains the given element.
return getArrayIndex(array, elem) != -1;
|
public static boolean | contains(T[] array, T elem)Returns true if the given {@code array} contains the given element.
return getArrayIndex(array, elem) != -1;
|
public static int[] | convertStringListToIntArray(java.util.List list, java.lang.String[] convertFrom, int[] convertTo)Create an {@code int[]} from the {@code List<>} by using {@code convertFrom} and
{@code convertTo} as a one-to-one map (via the index).
Strings not appearing in {@code convertFrom} are ignored (with a logged warning);
strings appearing in {@code convertFrom} but not {@code convertTo} are silently
dropped.
if (list == null) {
return null;
}
List<Integer> convertedList = convertStringListToIntList(list, convertFrom, convertTo);
int[] returnArray = new int[convertedList.size()];
for (int i = 0; i < returnArray.length; ++i) {
returnArray[i] = convertedList.get(i);
}
return returnArray;
|
public static java.util.List | convertStringListToIntList(java.util.List list, java.lang.String[] convertFrom, int[] convertTo)Create an {@code List} from the {@code List<>} by using {@code convertFrom} and
{@code convertTo} as a one-to-one map (via the index).
Strings not appearing in {@code convertFrom} are ignored (with a logged warning);
strings appearing in {@code convertFrom} but not {@code convertTo} are silently
dropped.
if (list == null) {
return null;
}
List<Integer> convertedList = new ArrayList<>(list.size());
for (String str : list) {
int strIndex = getArrayIndex(convertFrom, str);
// Guard against unexpected values
if (strIndex < 0) {
if (VERBOSE) Log.v(TAG, "Ignoring invalid value " + str);
continue;
}
// Ignore values we can't map into (intentional)
if (strIndex < convertTo.length) {
convertedList.add(convertTo[strIndex]);
}
}
return convertedList;
|
public static int | getArrayIndex(T[] array, T needle)Return the index of {@code needle} in the {@code array}, or else {@code -1}
if (array == null) {
return -1;
}
int index = 0;
for (T elem : array) {
if (Objects.equals(elem, needle)) {
return index;
}
index++;
}
return -1;
|
public static int | getArrayIndex(int[] array, int needle)Return the index of {@code needle} in the {@code array}, or else {@code -1}
if (array == null) {
return -1;
}
for (int i = 0; i < array.length; ++i) {
if (array[i] == needle) {
return i;
}
}
return -1;
|
public static int[] | toIntArray(java.util.List list)Convert the list of integers in {@code list} to an {@code int} array.
Every element in {@code list} must be non-{@code null}.
if (list == null) {
return null;
}
int[] arr = new int[list.size()];
int i = 0;
for (int elem : list) {
arr[i] = elem;
i++;
}
return arr;
|