java commons.lang3 ArrayUtils使用


java commons.lang3 ArrayUtils使用
import org.apache.commons.lang3.ArrayUtils;

/**
*數組追加數組,不重復
*/
public static int[] arrayAddArray(int[] src,int[] arr) {
// 查詢某個Object是否在數組中
// ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
int[] newarr = ArrayUtils.clone(src);
for (int i = 0; i < arr.length; i++) {
if( !ArrayUtils.contains(newarr,arr[i]) ){
newarr = ArrayUtils.add(newarr, arr[i]);
}
}
// System.out.println("---------------");
// for (int i = 0; i < newarr.length; i++) {
// System.out.println(newarr[i]);
// }
return newarr;
}

/**
*取數組在一個大小范圍內的值
*/
public static int[] getArea(int[] src,int[] minmaxNum) {
int slen = src.length;
int[] newarr = {};
for (int i = 0; i < slen; i++) {
if(src[i]>=minmaxNum[0] && src[i]<=minmaxNum[1]){
newarr = ArrayUtils.add(newarr, src[i]);
}
}
// System.out.println("---------------");
// for (int i = 0; i < newarr.length; i++) {
// System.out.println(newarr[i]);
// }
// ArrayUtils.reverse(newarr);//數組反轉
return newarr;
}
============================
ArrayUtils 擁有以下方法:
toString
將一個數組轉換成String,用於打印數組
isEquals
判斷兩個數組是否相等,采用EqualsBuilder進行判斷
toMap
將一個數組轉換成Map,如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
clone
拷貝數組
subarray
截取子數組
isSameLength
判斷兩個數組長度是否相等
getLength
獲得數組的長度
isSameType
判段兩個數組的類型是否相同
reverse
數組反轉
indexOf
查詢某個Object在數組中的位置,可以指定起始搜索位置
lastIndexOf
反向查詢某個Object在數組中的位置,可以指定起始搜索位置
contains
查詢某個Object是否在數組中
toObject
將基本數據類型轉換成外包型數據
isEmpty
判斷數組是否為空(null和length=0的時候都為空)
addAll
合並兩個數組
add
添加一個數據到數組
remove
刪除數組中某個位置上的數據
removeElement
刪除數組中某個對象(從正序開始搜索,刪除第一個)

// 1.打印數組
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.判斷兩個數組是否相等,采用EqualsBuilder進行判斷
// 只有當兩個數組的數據類型,長度,數值順序都相同的時候,該方法才會返回True
// 2.1 兩個數組完全相同
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 數據類型以及長度相同,但各個Index上的數據不是一一對應
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 數組的長度不一致
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 不同的數據類型
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處理,如果輸入的兩個數組都為null時候則返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true

// 3.將一個數組轉換成Map
// 如果數組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
// 對於Object[]數組里的元素必須是instanceof Object[]或者Entry,即不支持基本數據類型數組
// 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})會出異常
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.拷貝數組
ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}

// 5.截取數組
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之后的所有數據

// 6.判斷兩個數組的長度是否相等
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true

// 7.獲得數組的長度
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3

// 8.判段兩個數組的類型是否相同
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.數組反轉
int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array);// {5,2,1}

// 10.查詢某個Object在數組中的位置,可以指定起始搜索位置,找不到返回-1
// 10.1 從正序開始搜索,搜到就返回當前的index否則返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
// 10.2 從逆序開始搜索,搜到就返回當前的index否則返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2

// 11.查詢某個Object是否在數組中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
// 對於Object數據是調用該Object.equals方法進行判斷
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false

// 12.基本數據類型數組與外包型數據類型數組互轉
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.判斷數組是否為空(null和length=0的時候都為空)
ArrayUtils.isEmpty(new int[0]);// true
ArrayUtils.isEmpty(new Object[] { null });// false

// 14.合並兩個數組
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}

// 15.添加一個數據到數組
ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}

// 16.刪除數組中某個位置上的數據
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}

// 17.刪除數組中某個對象(從正序開始搜索,刪除第一個)
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM