ArrayUtils 工具类
ArrayUtils 是 Apache Commons Lang 3 工具包中常用的工具类,使用是需要依赖对应的lab,对应的maven引用如下:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
ArrayUtils提供了处理是数组的一些常用方法,包含基本类型数组,对象数组。做了一些非常漂亮的处理,来避免空值及其他类型的异常。
常用方法有
isEmpty 判断数组是否为空
toString 按数组对象输出
clone 拷贝
isNotEmpty 判断数组是否不为空
toPrimitive 包装类型数组转换成基本类型数组(Integer 包装类 --> int 基本类型)
indexOf 在数组中的下标
toObject 基本类型数组转换成对应包装类型数组
toMap 转换成Map (数组元素是 key:value成对对象)
add 和addAll 添加单个元素或添加一个数组中的所有元素
remove 和 removeAll 删除单个元素或删除一个数组中所有元素
removeElement 和 removeElements 删除第一个给的元素和删除第一个给的数组
removeAllOccurences 删除可能存在的元素(数组中可能有该元素也可能没有)
contains 是否包含元素
reverse 反转数组
shift 随机打乱数组
subarray 获取子数组
swap 交互数组指定位置元素
部分参考代码
package org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class PrimitiveArrayClone {
public static void main(String[] args) {
int[] fibonacci = new int[] {1, 1, 2, 3, 5, 8, 13, 21, 34, 55};
System.out.println("fibonacci = " + ArrayUtils.toString(fibonacci));
int[] clone = ArrayUtils.clone(fibonacci);
System.out.println("clone = " + ArrayUtils.toString(clone));
}
}
package org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayUtilsToString {
public static void main(String[] args) {
// Print int array as string.
int[] numbers = {1, 2, 3, 5, 8, 13, 21, 34};
System.out.println("Numbers = " + ArrayUtils.toString(numbers));
// Print string array as string.
String[] grades = {"A", "B", "C", "D", "E", "F"};
System.out.println("Grades = " + ArrayUtils.toString(grades));
// Print multidimensional array as string.
int[][] matrix = {{0, 1, 2}, {1, 2, 3}, {2, 3, 4}};
System.out.println("Matrix = " + ArrayUtils.toString(matrix));
// Return "Empty" when the array is null.
String[] colors = null;
System.out.println("Colors = " + ArrayUtils.toString(colors, "None"));
}
}
Numbers = {1,2,3,5,8,13,21,34}
Grades = {A,B,C,D,E,F}
Matrix = {{0,1,2},{1,2,3},{2,3,4}}
Colors = None
package org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayUtilsIndexOfDemo {
public static void main(String[] args) {
String[] colours = { "Red", "Orange", "Yellow", "Green",
"Blue", "Violet", "Orange", "Blue" };
// Does colours array contains the Blue colour?
boolean contains = ArrayUtils.contains(colours, "Blue");
System.out.println("Contains Blue? " + contains);
// Can you tell me the index of each colour defined bellow?
int indexOfYellow = ArrayUtils.indexOf(colours, "Yellow");
System.out.println("indexOfYellow = " + indexOfYellow);
int indexOfOrange = ArrayUtils.indexOf(colours, "Orange");
System.out.println("indexOfOrange = " + indexOfOrange);
int lastIndexOfOrange = ArrayUtils.lastIndexOf(colours, "Orange");
System.out.println("lastIndexOfOrange = " + lastIndexOfOrange);
}
}
package org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayObjectToPrimitiveDemo {
public static void main(String[] args) {
// An array of Integer objects.
Integer[] integers = {1, 2, 3, 5, 8, 13, 21, 34, 55};
Boolean[] booleans = {Boolean.TRUE, Boolean.TRUE, Boolean.FALSE, Boolean.FALSE};
// Convert array of Integer objects into array of type int.
int[] ints = ArrayUtils.toPrimitive(integers);
System.out.println(ArrayUtils.toString(ints));
// Convert array of Boolean objects into array of type boolean.
boolean[] bools = ArrayUtils.toPrimitive(booleans);
System.out.println(ArrayUtils.toString(bools));
}
}
package org.kodejava.example.commons.lang;
import java.util.Map;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayToMapExample {
public static void main(String[] args) {
// A two dimensional array of countries capital.
String[][] countries = {{"United States", "Washington, D.C."},
{"United Kingdom", "London"},
{"Netherlands", "Amsterdam"},
{"Japan", "Tokyo"},
{"France", "Paris"}};
// Convert an array to a Map.
Map capitals = ArrayUtils.toMap(countries);
for (Object key : capitals.keySet()) {
System.out.printf("%s is the capital of %s.%n", capitals.get(key), key);
}
}
}
package org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayReverseExample {
public static void main(String[] args) {
// Define colors array.
String[] colors = {"Red", "Green", "Blue", "Cyan", "Yellow", "Magenta"};
System.out.println(ArrayUtils.toString(colors));
// Now we reverse the order of array elements.
ArrayUtils.reverse(colors);
System.out.println(ArrayUtils.toString(colors));
}
}
ackage org.kodejava.example.commons.lang;
import org.apache.commons.lang3.ArrayUtils;
public class ArrayPrimitiveObjectConversionDemo {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
boolean[] booleans = {true, false, false, true};
float[] decimals = {10.1f, 3.14f, 2.17f};
Integer[] numbersObjects = ArrayUtils.toObject(numbers);
Boolean[] booleansObjects = ArrayUtils.toObject(booleans);
Float[] decimalsObjects = ArrayUtils.toObject(decimals);
numbers = ArrayUtils.toPrimitive(numbersObjects);
booleans = ArrayUtils.toPrimitive(booleansObjects);
decimals = ArrayUtils.toPrimitive(decimalsObjects);
}
}
int[] oldArray = { 2, 3, 4, 5 };
int[] newArray = ArrayUtils.add(oldArray, 0, 1);
int[] expectedArray = { 1, 2, 3, 4, 5 };
assertArrayEquals(expectedArray, newArray);
int[] oldArray = { 0, 1, 2 };
int[] newArray = ArrayUtils.addAll(oldArray, 3, 4, 5);
int[] expectedArray = { 0, 1, 2, 3, 4, 5 };
assertArrayEquals(expectedArray, newArray);
int[] oldArray = { 1, 2, 2, 2, 3 };
int[] newArray = ArrayUtils.removeAllOccurences(oldArray, 2);
int[] expectedArray = { 1, 3 };
assertArrayEquals(expectedArray, newArray);
int[] originalArray = { 1, 2, 3, 4, 5 };
ArrayUtils.reverse(originalArray);
int[] expectedArray = { 5, 4, 3, 2, 1 };
assertArrayEquals(expectedArray, originalArray);
int[] originalArray = { 1, 2, 3, 4, 5 };
ArrayUtils.shift(originalArray, 1);
int[] expectedArray = { 5, 1, 2, 3, 4 };
assertArrayEquals(expectedArray, originalArray);
int[] oldArray = { 1, 2, 3, 4, 5 };
int[] newArray = ArrayUtils.subarray(oldArray, 2, 7);
int[] expectedArray = { 3, 4, 5 };
assertArrayEquals(expectedArray, newArray);
int[] originalArray = { 1, 2, 3, 4, 5 };
ArrayUtils.swap(originalArray, 0, 3);
int[] expectedArray = { 4, 2, 3, 1, 5 };
assertArrayEquals(expectedArray, originalArray);
参考:
https://kodejava.org/tag/arrayutils/
https://www.codota.com/code/java/classes/org.apache.commons.lang.ArrayUtils
https://www.baeldung.com/array-processing-commons-lang
https://blog.csdn.net/yaomingyang/article/details/79127362
https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html