本文以3.7版本为例:
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>ArrayUtils.toString(new int[] { 1, 4, 2, 3 }); // {1,4,2,3} ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 }); // {1,4,2,3} ArrayUtils.toString(null, “I’m nothing!”); // I’m nothing!
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4); // {1,5} // 起始index为2(即第三个数据)结束index为4的数组 ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10); // {1,5,6} // 如果endIndex大于数组的长度,则取beginIndex之后的所有数据
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L }); // true
ArrayUtils.getLength(new long[] { 1, 23, 3 }); // 3
ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 }); // true ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 }); // false
int[] array = new int[] { 1, 2, 5 }; ArrayUtils.reverse(array); // {5,2,1}
// 从正序开始搜索,搜到就返回当前的index否则返回-1 ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6); // 2 ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2); // -1
lastIndexOf 反向查询某个Object在数组中的位置,可以指定起始搜索位置// 从逆序开始搜索,搜到就返回当前的index否则返回-1 ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6); // 2
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1); // true // 对于Object数据是调用该Object.equals方法进行判断 ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L); // false
ArrayUtils.toObject(new int[] { 1, 2 }); // new Integer[]{Integer,Integer} ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) }); // new int[]{1,2}
ArrayUtils.isEmpty(new int[0]); // true ArrayUtils.isEmpty(new Object[] { null }); // false
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 }); // {1,3,5,2,4}
ArrayUtils.add(new int[] { 1, 3, 5 }, 4); // {1,3,5,4}
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1); // {1,5}
removeElement 删除数组中某个对象(从正序开始搜索,删除第一个)ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3); // {1,5}