Class Arrays
java.lang.Object
ushiosan.jvm_utilities.lang.collection.Arrays
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intnumber to represent not found operation in the arrays -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean[]booleanOf(boolean... elements) Generate a primitive boolean arraystatic byte @NotNull []Generate a primitive byte arraystatic char[]charOf(char... elements) Generate a primitive char arraystatic booleanChecks if the array contains the selected element.static double[]doubleOf(double... elements) Generate a primitive double arraystatic float[]floatOf(float... elements) Generate a primitive float arraystatic intSearch elements in the arraystatic int[]intOf(int... elements) Generate a primitive int arraystatic intlastIndexOf(Object @NotNull [] array, @Nullable Object element) Search an element in the array.static long[]longOf(long... elements) Generate a primitive long arrayGenerates a generic array with numeric objects.Generate array from given values.static <T> T[]of(T... elements) Generate array from given values.static short @NotNull []Generate a primitive short arraytoNumberOf(byte[] array) Convert the primitive byte array to a numeric arraytoNumberOf(double[] array) Convert the primitive double array to a numeric arraytoNumberOf(float[] array) Convert the primitive float array to a numeric arraytoNumberOf(int[] array) Convert the primitive int array to a numeric arraytoNumberOf(long[] array) Convert the primitive long array to a numeric arraytoNumberOf(short[] array) Convert the primitive short array to a numeric arraytoObjectArray(Object array) Convert any array to object array.
-
Field Details
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUNDnumber to represent not found operation in the arrays- See Also:
-
-
Method Details
-
of
Generate array from given values. If you want to use primitive arrays, you must use the classes that encapsulate those types. -
of
Generate array from given values. If you want to use primitive arrays, you must use the classes that encapsulate those types. -
indexOf
@Contract(pure=true) public static int indexOf(Object @NotNull [] array, @Nullable @Nullable Object element) Search elements in the array- Parameters:
array- base array to searchelement- the element to search- Returns:
- the first index element or
-1if element not exists
-
lastIndexOf
@Contract(pure=true) public static int lastIndexOf(Object @NotNull [] array, @Nullable @Nullable Object element) Search an element in the array. This method tries to search in reverse, first starting at the end of the array, to find the desired element.- Parameters:
array- base array to searchelement- the element to search- Returns:
- the first index element or
-1if element not exists
-
contains
Checks if the array contains the selected element.- Parameters:
array- base array to searchelement- the element to search- Returns:
trueif the element exists orfalseotherwise
-
toObjectArray
Convert any array to object array. This also applies to primitive types.Be careful with this method because this method uses a trick to convert primitive types to numeric types and does not use wrapper classes. Instead, the
Numberclass is used for any numeric type (except for the char type).Examples
// For numeric primitives always use Number class Number[] intArr = (Number[]) Arrays.toObjectArray(new int[] {2, 4, 6, 8}); Number[] shortArr = (Number[]) Arrays.toObjectArray(new short[] {12, 21, 42, 120, 0xFF}); Number[] floatArr = (Number[]) Arrays.toObjectArray(new float[] {1f, 2f, 3f, 120.2234f}); // Wrapped clases Boolean[] boolArr = (Boolean[]) Arrays.toObjectArray(new boolean[] {true, false, false, true}); Character[] intArr = (Character[]) Arrays.toObjectArray(new char[] {'H', 'e', 'l', 'l', 'o'});- Parameters:
array- the array to convert- Returns:
- a converted array object
-
numberOf
Generates a generic array with numeric objects.- Parameters:
elements- numeric elements- Returns:
- an array with all numeric elements
-
booleanOf
public static boolean[] booleanOf(boolean... elements) Generate a primitive boolean array- Parameters:
elements- boolean elements- Returns:
- an array with all boolean elements
-
charOf
public static char[] charOf(char... elements) Generate a primitive char array- Parameters:
elements- char elements- Returns:
- an array with all char elements
-
byteOf
Generate a primitive byte array- Parameters:
elements- byte elements- Returns:
- an array with all bytes
-
toNumberOf
Convert the primitive byte array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-
shortOf
Generate a primitive short array- Parameters:
elements- short elements- Returns:
- an array with all shot elements
-
toNumberOf
Convert the primitive short array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-
intOf
public static int[] intOf(int... elements) Generate a primitive int array- Parameters:
elements- int elements- Returns:
- an array with all int elements
-
toNumberOf
Convert the primitive int array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-
longOf
public static long[] longOf(long... elements) Generate a primitive long array- Parameters:
elements- long elements- Returns:
- an array with all long elements
-
toNumberOf
Convert the primitive long array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-
floatOf
public static float[] floatOf(float... elements) Generate a primitive float array- Parameters:
elements- float elements- Returns:
- an array with all float elements
-
toNumberOf
Convert the primitive float array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-
doubleOf
public static double[] doubleOf(double... elements) Generate a primitive double array- Parameters:
elements- double elements- Returns:
- an array with double bytes
-
toNumberOf
Convert the primitive double array to a numeric array- Parameters:
array- the array to convert- Returns:
- an array with all numeric objects
- See Also:
-