catalogue
1. Introducing pom dependency
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency>
2. Methods in ArrayUtils:
1.add(): adds the given data to the specified array and returns a new array.
2.addAll(): merge two arrays.
3.contains(): check whether the data exists in the array and return a boolean value.
4.getLength(): returns the length of the array.
5.indexOf(): query whether there is a specified value in the array from the first bit of the array. If there is a value that returns index, otherwise - 1 is returned.
6.lastIndexOf(): query whether there is a specified value in the array from the last bit of the array. If there is a value that returns index, otherwise - 1 is returned.
7.Insert(): add the specified element to the array at the specified position and return a new array.
8.isEmpty(): judge whether the array is empty and return a boolean value.
9.isNotEmpty(): judge whether the array is empty, not null.
10.isSameLength(): judge whether the lengths of two arrays are the same. When the array is empty, the length is 0. Returns a boolean value.
11.isSameType(): judge whether the types of two arrays are the same and return a boolean value.
12.isSorted(): judge whether the array is sorted in natural order and return a boolean value.
13.nullToEmpty():
14.remove(): delete the element at the specified position of the array and return a new array.
15.removeAll(): deletes the element at the specified position and returns a new array.
16. Removealloccurrences(): deletes the specified element from the array and returns a new array.
17.removeElement(): deletes the first occurrence of the specified element from the array and returns a new array.
18.removeElements(): delete the specified number of elements from the array and return a new array.
19.reverse(): array inversion. You can also specify the start and end reversal positions.
20.subarray(): intercepts the array (header but not tail) and returns a new array.
21.swap(): specify the element exchange of two positions of the array, or specify the length element of len after two positions.
22.toMap(): convert an array into a map and return a collection of map objects.
23.toObject(): converts an array of original data types into an array of object types.
24.toPrimitive(): convert the object type array to the original data type array.
25.toString(): output the array as Stirng and return a string.
26.toStringArray(): converts an Object array to a String array type.
3. Examples
public class ArraryTest { public static void main(String[] args) { int []array={4,5,9}; //The result of the add() method is: {4,5,9,6} int[] newArray=ArrayUtils.add(array, 6); System.out.println(ArrayUtils.toString(newArray)); //addAll() method, the result is: {4,5,9,5,9,6,7} int []arrayAll={4,5,9}; int[] newArrayAll=ArrayUtils.addAll(arrayAll,5,9,6,7); System.out.println(ArrayUtils.toString(newArrayAll)); //contains(): the result is: true, false System.out.println(ArrayUtils.contains(arrayAll, 9)); System.out.println(ArrayUtils.contains(arrayAll, 3)); //getLength(): the result is 3 System.out.println(ArrayUtils.getLength(arrayAll)); //indexOf():2. //indexOf(newArrayAll, 9,3):3 specifies which bit to start searching and returns the result 4 System.out.println(ArrayUtils.indexOf(newArrayAll, 9)); System.out.println(ArrayUtils.indexOf(newArrayAll, 9,3)); //The returned results of lastIndexOf() are 4 and 2 System.out.println(ArrayUtils.lastIndexOf(newArrayAll, 9)); System.out.println(ArrayUtils.lastIndexOf(newArrayAll, 9,3)); //insert(): the result is {4,5,3,9} int [] arr=ArrayUtils.insert(2, arrayAll, 3); System.out.println("insert"+ArrayUtils.toString(arr)); //isEmpty(): the result is false and true int []a=null; System.out.println(ArrayUtils.isEmpty(arr)); System.out.println(ArrayUtils.isEmpty(a)); //isNotEmpty(): the result is false and true System.out.println("isNotEmpty:"+ArrayUtils.isNotEmpty(a)); System.out.println("isNotEmpty:"+ArrayUtils.isNotEmpty(arr)); //isSorted(): the result is false and true int[]sort1={5,6,9,1}; int [] sort2={1,6,8,9}; System.out.println("sort1:"+ArrayUtils.isSorted(sort1)); System.out.println("sort2:"+ArrayUtils.isSorted(sort2)); //remove(): the returned result is {5,6,1} int [] newRe=ArrayUtils.remove(sort1, 2); for(int nr:newRe){ System.out.print(nr); } //reverse(): return new reverse:{1,9,6,5} ArrayUtils.reverse(sort1); System.out.println("new reverse:"+ArrayUtils.toString(sort1)); //subarray(): return result subarray:{3,9} int[] sub={7,5,3,9,8,4}; int [] newsub=ArrayUtils.subarray(sub, 2, 4); System.out.println("subarray:"+ArrayUtils.toString(newsub)); Object[] subs={7,5,3,9,8,4}; Map<Object, Object>map=ArrayUtils.toMap(subs); } }