add -> 从数组后添加元素
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
使用效果
String[] stringArray = {"a", "b", "c", "d", "e"};
System.out.println(Arrays.toString(stringArray));
String[] fs = ArrayUtils.add(stringArray, "f");
System.out.println(Arrays.toString(fs));
[a, b, c, d, e]
[a, b, c, d, e, f]
PS:
ArrayUtils.add(null, null) = IllegalArgumentException
ArrayUtils.add(null, "a") = ["a"]
ArrayUtils.add(["a"], null) = ["a", null]
ArrayUtils.add(["a"], "b") = ["a", "b"]
ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
addAll -> 往数组里添加数组
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)、T (泛型)多参数
使用效果
@Test public void addAllPrac() {
String[] stringArray = {"a", "b", "c", "d", "e"};
String[] stringArray2 = {"a", "b", "c", "d", "e"};
String[] fs = ArrayUtils.addAll(stringArray, stringArray2);
System.out.println(Arrays.toString(fs));
}
[a, b, c, d, e, a, b, c, d, e]
ps
ArrayUtils.addAll(array1, null) = cloned copy of array1
ArrayUtils.addAll(null, array2) = cloned copy of array2
ArrayUtils.addAll([], []) = []
addFirst -> 从数组前添加元素
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
使用效果
String[] stringArray = {"a", "b", "c", "d", "e"};
String[] fs = ArrayUtils.addFirst(stringArray, "t");
System.out.println(Arrays.toString(fs));
[t, a, b, c, d, e]
PS
ArrayUtils.addFirst(null, true) = [true]
ArrayUtils.addFirst([true], false) = [false, true]
ArrayUtils.addFirst([true, false], true) = [true, true, false]
clone -> 克隆数组
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
使用效果
String[] stringArray = {"a", "b", "c", "d", "e"};
String[] fs = ArrayUtils.clone(stringArray);
System.out.println(Arrays.toString(fs));
[a, b, c, d, e]
contains -> 判断数组是否包含元素
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
注意double还有个指定范围内查询的方法
使用效果
String[] stringArray = {"a", "b", "c", "d", "e"};
boolean a = ArrayUtils.contains(stringArray, "a");
System.out.println(a);
true
get -> 获取下标对应的元素、另一个方法可以指定默认值
getLength
hashCode
image.png
indexesOf
->Finds the indices of the given value in the array
->Finds the indices of the given value in the array starting at the given index.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
使用效果
String[] stringArray = {"a", "b", "c", "d", "a"};
BitSet a = ArrayUtils.indexesOf(stringArray, "a");
System.out.println(a);
{0, 4}
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
BitSet a = ArrayUtils.indexesOf(stringArray, "a");
System.out.println(a);
BitSet b = ArrayUtils.indexesOf(stringArray, "a", 1);
System.out.println(b);
{0, 1, 5}
{1, 5}
针对double的情况
// 查找3D这个值得所有下标
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};
BitSet e = ArrayUtils.indexesOf(arr, 2D);
System.out.println(e);
{1, 5}
// 从下标为1开始查找3D这个值得所有下标
double[] arr = {1D, 2D, 3D, 4D, 5D, 1D};
BitSet d = ArrayUtils.indexesOf(arr, 3D, 1);
System.out.println(d);
BitSet d1 = ArrayUtils.indexesOf(arr, 3D, 2);
System.out.println(d1);
BitSet d2 = ArrayUtils.indexesOf(arr, 3D, 3);
System.out.println(d2);
{2}
{2}
{}
// 找出3-1,3,3+1的下标的位置
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};
BitSet c = ArrayUtils.indexesOf(arr, 3D, 1D);
System.out.println(c);
{1, 2, 3, 5}
// 从下标为1开始查找、找出3-1,3,3+1的下标的位置
double[] arr = {1D, 2D, 3D, 4D, 5D, 2D};
BitSet f = ArrayUtils.indexesOf(arr, 3D, 2, 1D);
System.out.println(f);
{2, 3, 5}
indexof : 查找元素第一次出现的下标
-> Finds the index of the given object in the array.
-> Finds the index of the given object in the array starting at the given index.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
int a = ArrayUtils.indexOf(stringArray, "a");
System.out.println(a);
0
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
int b = ArrayUtils.indexOf(stringArray, "a",1);
System.out.println(b);
1
lastIndexOf
-> Finds the last index of the given value within the array.
-> Finds the last index of the given value in the array starting at the given index.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
insert ->Inserts elements into an array at the given index (starting from zero).
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
ArrayUtils.insert(index, null, null) = null
ArrayUtils.insert(index, array, null) = cloned copy of 'array'
ArrayUtils.insert(index, null, values) = null
使用效果
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
String[] zs = ArrayUtils.insert(1, stringArray, "z");
System.out.println(Arrays.toString(stringArray));
System.out.println(Arrays.toString(zs));
[a, a, b, c, d, a]
[a, z, a, b, c, d, a]
String[] stringArray = {"a", "a", "b", "c", "d", "a"};
String[] zs = ArrayUtils.insert(1, stringArray, "z", "s");
System.out.println(Arrays.toString(stringArray));
System.out.println(Arrays.toString(zs));
[a, a, b, c, d, a]
[a, z, s, a, b, c, d, a]
isArrayIndexValid ->Returns whether a given array can safely be accessed at the given index.
ArrayUtils.isArrayIndexValid(null, 0) = false
ArrayUtils.isArrayIndexValid([], 0) = false
ArrayUtils.isArrayIndexValid(["a"], 0) = true
使用效果
String[] stringArray = {"a", "a"};
boolean arrayIndexValid = ArrayUtils.isArrayIndexValid(stringArray, 2);
System.out.println(arrayIndexValid);
false
isEmpty ->Checks if an array of primitive booleans is empty or null
isNotEmpty ->Checks if an array of primitive booleans is not empty and not null
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
- T(泛型)
使用效果
String[] stringArray = {"a", "a"};
boolean a = ArrayUtils.isEmpty(stringArray);
boolean b = ArrayUtils.isNotEmpty(stringArray);
System.out.println(a);
System.out.println(b);
false
true
isSameLength -> Checks whether two arrays are the same length, treating arrays as length 0
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object 一个为元素类型、一个就为Object
isSameType -> Checks whether two arrays are the same type taking into account multi-dimensional arrays.
isSorted ->
This method checks whether the provided array is sorted according to natural ordering
false before true
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
使用效果
String[] stringArray = {"a", "a"};
boolean a = ArrayUtils.isSorted(stringArray);
System.out.println(a);
String[] arr = {"a", "b"};
boolean b = ArrayUtils.isSorted(arr);
System.out.println(b);
String[] arr1 = {"a", "b", "a"};
boolean c = ArrayUtils.isSorted(arr1);
System.out.println(c);
true
true
false
nullToEmpty -> Defensive programming technique to change a null reference to an empty one.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Byte、Short、Integer、Long、Float、Double、Boolean、Character
- T(泛型)
- Object
- Class
remove
-> Removes the element at the specified position from the specified array. All subsequent elements are shifted to the left
-> Removes the elements at the specified positions from the specified array. All remaining elements are shifted to the left.
ArrayUtils.remove(["a"], 0) = []
ArrayUtils.remove(["a", "b"], 0) = ["b"]
ArrayUtils.remove(["a", "b"], 1) = ["a"]
ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
ArrayUtils.removeAll([1], 0) = []
ArrayUtils.removeAll([2, 6], 0) = [6]
ArrayUtils.removeAll([2, 6], 0, 1) = []
ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
removeAllOccurrences -> Removes the occurrences of the specified element from the specified int array.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
removeElement -> Removes the first occurrence of the specified element from thespecified array. All subsequent elements are shifted to the left. If the array doesn't contains such an element, no elements are removed from the array.
ArrayUtils.removeElement(null, true) = null
ArrayUtils.removeElement([], true) = []
ArrayUtils.removeElement([true], false) = [true]
ArrayUtils.removeElement([true, false], false) = [true]
ArrayUtils.removeElement([true, false, true], true) = [false, true]
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
reverse -> Reverses the order of the given array in the given range.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
shift -> Shifts the order of the given int array.
->Shifts the order of the given int array.
->Shifts the order of a series of elements in the given int array.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
使用效果
String[] stringArray1 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray1,1);
System.out.println(Arrays.toString(stringArray1));
String[] stringArray = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray,2);
System.out.println(Arrays.toString(stringArray));
String[] stringArray2 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray2,3);
System.out.println(Arrays.toString(stringArray2));
String[] stringArray3 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray3,4);
System.out.println(Arrays.toString(stringArray3));
String[] stringArray4 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray4,5);
System.out.println(Arrays.toString(stringArray4));
String[] stringArray5 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(stringArray5,6);// 6%5 ->1 取模
System.out.println(Arrays.toString(stringArray5));
[e, a, b, c, d]
[d, e, a, b, c]
[c, d, e, a, b]
[b, c, d, e, a]
[a, b, c, d, e]
[e, a, b, c, d]
String[] arr1 = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(arr1,1,3,1);
System.out.println(Arrays.toString(arr1));
String[] arr = {"a", "b", "c", "d", "e"};
ArrayUtils.shift(arr,1,3,2);
System.out.println(Arrays.toString(arr));
[a, c, b, d, e]
[a, b, c, d, e]
shuffle -> Randomly permutes the elements of the specified array using the Fisher-Yates algorithm.
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
subarray -> Produces a new byte array containing the elements between the start and end indices. 左闭右开
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)
使用效果
String[] stringArray1 = {"a", "b", "c", "d", "e"};
String[] subarray = ArrayUtils.subarray(stringArray1, 1, 3);
System.out.println(Arrays.toString(subarray));
[b, c]
swap
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
-> Swaps two elements in the given boolean array.
ArrayUtils.swap(["1", "2", "3"], 0, 2) -> ["3", "2", "1"]
ArrayUtils.swap(["1", "2", "3"], 0, 0) -> ["1", "2", "3"]
ArrayUtils.swap(["1", "2", "3"], 1, 0) -> ["2", "1", "3"]
ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ["1", "2", "3"]
ArrayUtils.swap(["1", "2", "3"], -1, 1) -> ["2", "1", "3"]
-> Swaps a series of elements in the given int array.
ArrayUtils.swap(["1", "2", "3", "4"], 0, 2, 1) -> ["3", "2", "1", "4"]
ArrayUtils.swap(["1", "2", "3", "4"], 0, 0, 1) -> ["1", "2", "3", "4"]
ArrayUtils.swap(["1", "2", "3", "4"], 2, 0, 2) -> ["3", "4", "1", "2"]
ArrayUtils.swap(["1", "2", "3", "4"], -3, 2, 2) -> ["3", "4", "1", "2"]
ArrayUtils.swap(["1", "2", "3", "4"], 0, 3, 3) -> ["4", "2", "3", "1"]
toArray -> Create a type-safe generic array.
使用效果
String[] strings = ArrayUtils.toArray("a", "b", "c", "d", "e");
System.out.println(Arrays.toString(strings));
[a, b, c, d, e]
toMap -> Converts the given array into a Map
Map colorMap = ArrayUtils.toMap(new String[][] {
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}});
System.out.println(colorMap);
{RED=#FF0000, GREEN=#00FF00, BLUE=#0000FF}
toObject -> Converts an array of primitive ints to objects.
- byte、 short、 int、 long、 float、 double、 boolean 、char
toPrimitive
-> Converts an array of object Booleans to primitives.
-> Converts an array of object Booleans to primitives handling null
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
toString -> Outputs an array as a String, treating null as an empty array.
使用效果
String[] strings = ArrayUtils.toArray("a", "b", "c", "d", "e");
System.out.println(Arrays.toString(strings));
System.out.println(ArrayUtils.toString(strings));
[a, b, c, d, e]
{a,b,c,d,e}