This class actually has more than 8,000 lines, too unscientific, but the analysis that a large number of code is repeated, change the type (8 basic types, in addition to part of the code, all repeat, so the following example code gives only the implementation of type int and the implementation of object type) property Various types of empty arrays, including empty object array, empty class array, empty string array, 8 large base type array, 8 large base class encapsulate class array, the naming rules for these arrays are Empty_xxx_array, where the meaning of XXX is: Object , Class (Class), base type (BYTE, CHAR, short, INT, LONG, FLOAT, DOUBLE, BOOLEAN), base type package type (byte_object , Character_object, Short_object, Integer_object, Long_object, Float_object, Double_object,BOOLEAN_OBJECT)
Examples are as follows:
public static final object[] Empty_object_array = new Object[0] public
static final class<?>[] Empty_class_arra Y = new Class[0] public
static final string[] Empty_string_array = new String[0] public
static final long[] Empty_ Long_array = new Long[0] public
static final long[] Empty_long_object_array = new Long[0]
A flag used to indicate that an element is not in an array.
Index_not_found:
public static final int index_not_found =-1;
Construction Method
This method is used for JavaBean, and the user should not create arrayutils instances because the Arrayutils method is static
Public Arrayutils () {
super ();
}
Related Methods
tostring MethodToString (final Object array), when the array is null, returns "{}"
Core code return
toString (array, "{}");
ToString (final Object array, final String stringifnull), calling Tostringbuilder implementation
Core code
if (array = = NULL) {return
stringifnull;
}
return new Tostringbuilder (array, Tostringstyle.simple_style). Append (Array). ToString ();
Hashcode MethodHashcode (final Object array), calling Hashcodebuilder implementation
Core code return
new Hashcodebuilder (). Append (Array). Tohashcode ();
Tomap MethodMap<object, object> Tomap (final object[] array), accepts two types of array elements:
map.entry<?,? > object[], the number of elements is greater than or equal to 2, only the first second element, the first as the key, the second as value
Core code
final Map<object, object> Map = new hashmap<> (int) (ARRAY.LENGTH * 1.5));
for (int i = 0; i < Array.Length i++) {
Final object = Array[i];
If (object instanceof map.entry<?,? >) {
final map.entry<?,? > Entry = (map.entry<?,? >) object;
Map.put (Entry.getkey (), Entry.getvalue ());
} else if (object instanceof object[]) {
final object[] entry = (object[]) object;
if (Entry.length < 2) {
throw new illegalargumentexception ("Array element" + i + ", '"
+ object
+ ", ha s a length less than 2 ");
}
Map.put (Entry[0], entry[1]);
} else {
throw new illegalargumentexception ("Array element" + i + ", '
+ object
+" ', is neither of type Ma P.entry nor an Array ");
}
return map;
ToArray MethodToArray (final T ... items) converts the input parameters to an array
Clone Method
Call Array's Clone method implementation, with a null-and-void operation
Core code return
array = = null null:array.clone ();
Nulltoempty MethodNulltoempty (final t[] array, final Class
Core code
if (array = = NULL) {return
type.cast (Array.newinstance (Type.getcomponenttype (), 0))
return array;
Nulltoempty (final t[] array) converts an array of null or length 0 to an empty array object of the same type, and returns an array object if it is not empty
Core code return
IsEmpty (array)? Empty_object_array:array;
Subarray MethodThe core is to invoke the System.arraycopy method, startindexinclusive less than 0 starts from 0, endindexexclusive is greater than array.length, then to the Array.Length cutoff
Core code
final int newsize = endindexexclusive-startindexinclusive;
Final class<?> type = Array.getclass (). Getcomponenttype ();
if (newsize <= 0) {
@SuppressWarnings ("unchecked")//OK, because array is of type T
final t[] Emptyarray = (t[ ]) array.newinstance (type, 0);
return emptyarray;
}
@SuppressWarnings ("unchecked")//OK, because array is of type T
final
t[] Subarray = (t[]) array.newinstance (type , newsize);
System.arraycopy (Array, startindexinclusive, Subarray, 0, newsize);
return subarray;
Issamelength Method
Determine whether two array lengths are the same
Core code return
getlength (array1) = = GetLength (array2);
public static int GetLength (final Object array) {return
array = = NULL 0:array.getlength (array);
}
Issametype Method
public static Boolean Issametype (Final object array1, final object array2) {
if (array1 = = NULL | | array2 = NULL) {
throw New IllegalArgumentException ("The Array must not being null");
}
Return Array1.getclass (). GetName (). Equals (Array2.getclass (). GetName ());
Reverse MethodThe core method is to exchange the first element with the Lastindex-i element
public static void reverse (final object[] array) {
if (array = = NULL) {return
;
}
Reverse (array, 0, array.length);
}
public static void reverse (final boolean[] array, final int startindexinclusive, final int endindexexclusive) {
if (AR Ray = = null) {return
;
}
int i = startindexinclusive < 0? 0:startindexinclusive;
Int J = math.min (Array.Length, endindexexclusive)-1;
Boolean tmp;
while (J > i) {
tmp = array[j];
ARRAY[J] = Array[i];
Array[i] = tmp;
j--;
i++
}
}
Swap MethodSwap position of two element values
public static void Swap (final object[] array, final int offset1, final int offset2) {
if (array = = NULL | | array.lengt H = = 0) {return
;
}
Swap (array, Offset1, Offset2, 1);
}
Interchange Len elements starting with Offset1 and Offset2 len
= math.min (Math.min (len, Array.length-offset1), Array.Length- OFFSET2);
for (int i = 0; i < len; i++, offset1++, offset2++) {
final Boolean aux = Array[offset1];
ARRAY[OFFSET1] = Array[offset2];
Array[offset2] = aux;
}
Shift MethodIn the range of the child array to the right loop n elements, its implementation is to call the swap method, than to rotate the left len-n child elements, and then rotate the right n elements, and finally rotate all Len elements 3 times times slower
public static void Shift (final object[] array, final int offset) {if (array = = null) {return;
Shift (Array, 0, array.length, offset); The original call to swap is omitted/given rotate way to implement, faster than the swap mode 3 times times public static void rotate (int[) array, int startindexinclusive, int endindexe
xclusive, int offset) {rotate (array, startindexinclusive, Endindexexclusive-offset);
Rotate (array, endindexexclusive-offset, endindexexclusive);
Rotate (array, startindexinclusive, endindexexclusive); public static void rotate (int[] array, int startindexinclusive, int endindexexclusive) {if (array = null | | array . length = = 0 | | Startindexinclusive >= Array.Length | |
Endindexexclusive >= array.length) {return;
} if (Startindexinclusive < 0) {startindexinclusive = 0;
} if (Endindexexclusive < 0) {endindexexclusive = 0;
int low = startindexinclusive;
int high = endIndexExclusive-1; While [low < high] {int tmp = Array[low];
Array[low] = Array[high];
Array[high] = tmp;
low++;
high--; }
}
indexOf, LastIndexOf methodIterate over the array, can't find it returns a index_not_found floating-point type with one more precision parameter to determine what range of deviations is equal the last parameter startindex defaults to 0 and, if so, indicates where to start the search
int indexOf (final boolean[] array, final boolean valuetofind, int startIndex)
contains MethodCall the IndexOf method to determine if the return value is Index_not_found
Toprimitive MethodConverts an array of enclosure types to a base type array, the first method element cannot be empty (because there is a call to fetch the method), or it throws a npe; the second method can be converted to a set default value when the element is empty
element cannot be empty public
static char[] toprimitive (final character[] array) {
if (array = = NULL) {return
null;
} E LSE if (Array.Length = 0) {return
empty_char_array;
}
Final char[] result = new Char[array.length];
for (int i = 0; i < Array.Length i++) {
Result[i] = Array[i].charvalue ();
}
return result;
}
element can be empty, with valuefornull padding
//Core code for
(int i = 0; i < Array.Length; i++) {
final Character B = array[i];
Result[i] = (b = = null valuefornull:b.charvalue ());
}
return result;
Toobject MethodConverts a base type array into an array of enclosure types
public static character[] Toobject (final char[] array) {
if (array = = NULL) {return
null;
} else if (array.le Ngth = = 0) {return
empty_character_object_array;
}
Final character[] result = new Character[array.length];
for (int i = 0; i < Array.Length i++) {
Result[i] = character.valueof (array[i));
return result;
}
IsEmpty method and Isnotempty methodIsEmpty to determine whether the array is null or whether the length is 0 isnotempty the opposite of IsEmpty
AddAll MethodCreates a new array that contains all the elements of the passed-in parameter array the core is the System.arraycopy method
Add MethodCreates a new array with the length of the original array length plus 1, and the new array holds all its elements in the order of the original array. Inserts a new element in the last position of the new array
Remove MethodDeletes the element called System.arraycopy implementation at the specified location
Core code public
static int[] Remove (final int[] array, final int index) {return
(int[]) remove (Object) array, Inde x);
}
Private static object Remove (final Object array, final int index) {
final int length = getlength (array);
if (Index < 0 | | | Index >= length) {
throw new Indexoutofboundsexception ("index:" + index + ", Length:" + lengt h);
}
Final Object result = Array.newinstance (Array.getclass (). Getcomponenttype (), length-1);
System.arraycopy (array, 0, result, 0, index);
if (Index < length-1) {
system.arraycopy (array, index + 1, result, index, length-index-1);
}
return result;
}
Removeelement method; A bulk deletion (call RemoveAll Implementation) RemoveAll method deletes the element at the specified location, and you can delete multiple
Removeelement MethodLocate the first occurrence of the element you want to delete, and then call the Remove method to delete the element
final int index = indexOf (array, Element); if (index = = index_not_found) {return Clone (array
); Return remove (array, index);
public static short[] removeelements (final short[] array, final short ... values) {if (isempt Y (array) | |
IsEmpty (values)) {return clone (array);
Final Hashmap<short, mutableint> occurrences = new hashmap<> (values.length);
For [final short v:values] {final short boxed = short.valueof (v);
Final Mutableint count = Occurrences.get (boxed);
if (count = = null) {Occurrences.put (Boxed, new Mutableint (1));
else {count.increment ();
} final Bitset toremove = new Bitset ();
for (int i = 0; i < Array.Length. i++) {final short key = Array[i];
Final Mutableint count = Occurrences.get (key);
if (count!= null) {if (count.decrementandget () = = 0) {occurrences.remove (key);
} toremove.set (i);
} return (short[]) RemoveAll (array, toremove); }
RemoveAll Method
public static byte[] RemoveAll (final byte[] array, final int ... indices) {return
(byte[]) RemoveAll ((Object) array, I ndices);
}
Static Object RemoveAll (final Object array, final int ... indices) {
final int length = getlength (array);
int diff = 0; Number of distinct indexes, i.e. number of entries that'll be removed
//indices copy and sort
/identify length of Result array
if (Isnotempty (clonedindices)) {//
calculate need to delete the number of elements diff
}
//create result array, and loop call system.arraycopy return result
;
}
Removeelements MethodFind all the locations where you want to delete the elements, and then call the RemoveAll method
public static byte[] removeelements (final byte[] array, final byte ... values) {if IsEmpty (a Rray) | |
IsEmpty (values)) {return clone (array);
Final Map<byte, mutableint> occurrences = new hashmap<> (values.length);
For (final byte v:values) {final byte boxed = byte.valueof (v);
Final Mutableint count = Occurrences.get (boxed);
if (count = = null) {Occurrences.put (Boxed, new Mutableint (1));
else {count.increment ();
} final Bitset toremove = new Bitset ();
for (int i = 0; i < Array.Length i++) {final byte key = Array[i];
Final Mutableint count = Occurrences.get (key);
if (count!= null) {if (count.decrementandget () = = 0) {occurrences.remove (key);
} toremove.set (i);
} return (byte[]) RemoveAll (array, toremove); }
issorted MethodDetermines whether an array is not in descending order an empty array and an array of length less than 2 is not descending
Core code
Boolean previous = array[0];
Final int n = array.length;
for (int i = 1; i < n; i++) {
Final Boolean current = Array[i];
if (Booleanutils.compare (Previous, current) > 0) {return
false;
}
previous = current;
}
return true;
Removealloccurences MethodMoves all occurrences of the specified element in the array to find the location of all elements that meet the criteria, and then calls the RemoveAll
Core code return
RemoveAll (array, arrays.copyof (indices, count));
TostringarrayConvert to an array of strings, and if the element is empty, run out of NPE, but if you specify a default value when the element is empty, populate with the default values
Core code 1, element cannot be empty
//string[] Tostringarray (final object[] array)
final string[] result = new String[array.length] ;
for (int i = 0; i < Array.Length i++) {
Result[i] = array[i].tostring ();
}
Core code 2, with fill
//string[] tostringarray (final object[] array, final String valuefornullelements)
final string[] result = new String[array.length];
for (int i = 0; i < Array.Length i++) {
Final object = Array[i];
Result[i] = (Object = = null valueForNullElements:object.toString ());
}
Insert MethodCreate a new array
Call the System.arraycopy method to copy all elements before the original array insertion position insert the new element at the specified position call the System.arraycopy method copy the remaining elements of the original array
Core code
//Copy the position of all elements to be inserted into the new array index
system.arraycopy (values, 0, result, index, values.length);
Copy the original array before the index element to the new array
if (Index > 0) {
system.arraycopy (array, 0, result, 0, index);
}
Copy the original array index and subsequent elements to the new array
if (Index < array.length) {
system.arraycopy (array, index, result, index + Values.length, Array.length-index);
Shuffle MethodDisrupting the array elements, manipulating the array on the original array, swapping the elements in the randomly generated positions