ArrayUtils 擁有以下方法:
- toString
- 將一個(gè)數(shù)組轉(zhuǎn)換成String,用于打印數(shù)組
- isEquals
- 判斷兩個(gè)數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
- toMap
- 將一個(gè)數(shù)組轉(zhuǎn)換成Map,如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
- clone
- 拷貝數(shù)組
- subarray
- 截取子數(shù)組
- isSameLength
- 判斷兩個(gè)數(shù)組長(zhǎng)度是否相等
- getLength
- 獲得數(shù)組的長(zhǎng)度
- isSameType
- 判段兩個(gè)數(shù)組的類型是否相同
- reverse
- 數(shù)組反轉(zhuǎn)
- indexOf
- 查詢某個(gè)Object在數(shù)組中的位置,可以指定起始搜索位置
- lastIndexOf
- 反向查詢某個(gè)Object在數(shù)組中的位置,可以指定起始搜索位置
- contains
- 查詢某個(gè)Object是否在數(shù)組中
- toObject
- 將基本數(shù)據(jù)類型轉(zhuǎn)換成外包型數(shù)據(jù)
- isEmpty
- 判斷數(shù)組是否為空(null和length=0的時(shí)候都為空)
- addAll
- 合并兩個(gè)數(shù)組
- add
- 添加一個(gè)數(shù)據(jù)到數(shù)組
- remove
- 刪除數(shù)組中某個(gè)位置上的數(shù)據(jù)
- removeElement
- 刪除數(shù)組中某個(gè)對(duì)象(從正序開(kāi)始搜索,刪除第一個(gè))
eg:
// 1.打印數(shù)組
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!
// 2.判斷兩個(gè)數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
// 只有當(dāng)兩個(gè)數(shù)組的數(shù)據(jù)類型,長(zhǎng)度,數(shù)值順序都相同的時(shí)候,該方法才會(huì)返回True
// 2.1 兩個(gè)數(shù)組完全相同
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 數(shù)據(jù)類型以及長(zhǎng)度相同,但各個(gè)Index上的數(shù)據(jù)不是一一對(duì)應(yīng)
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 數(shù)組的長(zhǎng)度不一致
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 不同的數(shù)據(jù)類型
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new long[] { 1, 2, 3 });// false
ArrayUtils.isEquals(new Object[] { 1, 2, 3 }, new Object[] { 1, (long) 2, 3 });// false
// 2.5 Null處理,如果輸入的兩個(gè)數(shù)組都為null時(shí)候則返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true
// 3.將一個(gè)數(shù)組轉(zhuǎn)換成Map
// 如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
// 對(duì)于Object[]數(shù)組里的元素必須是instanceof Object[]或者Entry,即不支持基本數(shù)據(jù)類型數(shù)組
// 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})會(huì)出異常
ArrayUtils.toMap(new Object[] { new Object[] { 1, 2 }, new Object[] { 3, 4 } });// {1=2,
// 3=4}
ArrayUtils.toMap(new Integer[][] { new Integer[] { 1, 2 }, new Integer[] { 3, 4 } });// {1=2,
// 3=4}
// 4.拷貝數(shù)組
ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}
// 5.截取數(shù)組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4);// {1,5}
// 起始index為2(即第三個(gè)數(shù)據(jù))結(jié)束index為4的數(shù)組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10);// {1,5,6}
// 如果endIndex大于數(shù)組的長(zhǎng)度,則取beginIndex之后的所有數(shù)據(jù)
// 6.判斷兩個(gè)數(shù)組的長(zhǎng)度是否相等
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
// 7.獲得數(shù)組的長(zhǎng)度
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
// 8.判段兩個(gè)數(shù)組的類型是否相同
ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 });// true
ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 });// false
// 9.數(shù)組反轉(zhuǎn)
int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array);// {5,2,1}
// 10.查詢某個(gè)Object在數(shù)組中的位置,可以指定起始搜索位置,找不到返回-1
// 10.1 從正序開(kāi)始搜索,搜到就返回當(dāng)前的index否則返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
// 10.2 從逆序開(kāi)始搜索,搜到就返回當(dāng)前的index否則返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2
// 11.查詢某個(gè)Object是否在數(shù)組中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
// 對(duì)于Object數(shù)據(jù)是調(diào)用該Object.equals方法進(jìn)行判斷
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false
// 12.基本數(shù)據(jù)類型數(shù)組與外包型數(shù)據(jù)類型數(shù)組互轉(zhuǎn)
ArrayUtils.toObject(new int[] { 1, 2 });// new Integer[]{Integer,Integer}
ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}
// 13.判斷數(shù)組是否為空(null和length=0的時(shí)候都為空)
ArrayUtils.isEmpty(new int[0]);// true
ArrayUtils.isEmpty(new Object[] { null });// false
// 14.合并兩個(gè)數(shù)組
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
// 15.添加一個(gè)數(shù)據(jù)到數(shù)組
ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}
// 16.刪除數(shù)組中某個(gè)位置上的數(shù)據(jù)
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 17.刪除數(shù)組中某個(gè)對(duì)象(從正序開(kāi)始搜索,刪除第一個(gè))
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}