ArrayUtils 鎷ユ湁浠ヤ笅鏂規硶:
- toString
- 灝嗕竴涓暟緇勮漿鎹㈡垚String,鐢ㄤ簬鎵撳嵃鏁扮粍
- isEquals
- 鍒ゆ柇涓や釜鏁扮粍鏄惁鐩哥瓑,閲囩敤EqualsBuilder榪涜鍒ゆ柇
- toMap
- 灝嗕竴涓暟緇勮漿鎹㈡垚Map,濡傛灉鏁扮粍閲屾槸Entry鍒欏叾Key涓嶸alue灝辨槸鏂癕ap鐨凨ey鍜孷alue,濡傛灉鏄疧bject[]鍒橭bject[0]涓篕eyObject[1]涓篤alue
- clone
- 鎷瘋礉鏁扮粍
- subarray
- 鎴彇瀛愭暟緇?/dd>
- isSameLength
- 鍒ゆ柇涓や釜鏁扮粍闀垮害鏄惁鐩哥瓑
- getLength
- 鑾峰緱鏁扮粍鐨勯暱搴?/dd>
- isSameType
- 鍒ゆ涓や釜鏁扮粍鐨勭被鍨嬫槸鍚︾浉鍚?/dd>
- reverse
- 鏁扮粍鍙嶈漿
- indexOf
- 鏌ヨ鏌愪釜Object鍦ㄦ暟緇勪腑鐨勪綅緗?鍙互鎸囧畾璧峰鎼滅儲浣嶇疆
- lastIndexOf
- 鍙嶅悜鏌ヨ鏌愪釜Object鍦ㄦ暟緇勪腑鐨勪綅緗?鍙互鎸囧畾璧峰鎼滅儲浣嶇疆
- contains
- 鏌ヨ鏌愪釜Object鏄惁鍦ㄦ暟緇勪腑
- toObject
- 灝嗗熀鏈暟鎹被鍨嬭漿鎹㈡垚澶栧寘鍨嬫暟鎹?/dd>
- isEmpty
- 鍒ゆ柇鏁扮粍鏄惁涓虹┖(null鍜宭ength=0鐨勬椂鍊欓兘涓虹┖)
- addAll
- 鍚堝茍涓や釜鏁扮粍
- add
- 娣誨姞涓涓暟鎹埌鏁扮粍
- remove
- 鍒犻櫎鏁扮粍涓煇涓綅緗笂鐨勬暟鎹?/dd>
- removeElement
- 鍒犻櫎鏁扮粍涓煇涓璞?浠庢搴忓紑濮嬫悳绱?鍒犻櫎絎竴涓?
eg:
// 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榪涜鍒ゆ柇
// 鍙湁褰撲袱涓暟緇勭殑鏁版嵁綾誨瀷,闀垮害,鏁板奸『搴忛兘鐩稿悓鐨勬椂鍊?璇ユ柟娉曟墠浼氳繑鍥濼rue
// 2.1 涓や釜鏁扮粍瀹屽叏鐩稿悓
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 鏁版嵁綾誨瀷浠ュ強闀垮害鐩稿悓,浣嗗悇涓狪ndex涓婄殑鏁版嵁涓嶆槸涓涓瀵瑰簲
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 鏁扮粍鐨勯暱搴︿笉涓鑷?/span>
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 涓嶅悓鐨勬暟鎹被鍨?/span>
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澶勭悊,濡傛灉杈撳叆鐨勪袱涓暟緇勯兘涓簄ull鏃跺欏垯榪斿洖true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true
// 3.灝嗕竴涓暟緇勮漿鎹㈡垚Map
// 濡傛灉鏁扮粍閲屾槸Entry鍒欏叾Key涓嶸alue灝辨槸鏂癕ap鐨凨ey鍜孷alue,濡傛灉鏄疧bject[]鍒橭bject[0]涓篕eyObject[1]涓篤alue
// 瀵逛簬Object[]鏁扮粍閲岀殑鍏冪礌蹇呴』鏄痠nstanceof Object[]鎴栬匛ntry,鍗充笉鏀寔鍩烘湰鏁版嵁綾誨瀷鏁扮粍
// 濡?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涓?(鍗崇涓変釜鏁版嵁)緇撴潫index涓?鐨勬暟緇?/span>
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10);// {1,5,6}
// 濡傛灉endIndex澶т簬鏁扮粍鐨勯暱搴?鍒欏彇beginIndex涔嬪悗鐨勬墍鏈夋暟鎹?br />
// 6.鍒ゆ柇涓や釜鏁扮粍鐨勯暱搴︽槸鍚︾浉絳?/span>
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
// 7.鑾峰緱鏁扮粍鐨勯暱搴?/span>
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
// 8.鍒ゆ涓や釜鏁扮粍鐨勭被鍨嬫槸鍚︾浉鍚?/span>
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鍜宭ength=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.鍒犻櫎鏁扮粍涓煇涓綅緗笂鐨勬暟鎹?/span>
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 17.鍒犻櫎鏁扮粍涓煇涓璞?浠庢搴忓紑濮嬫悳绱?鍒犻櫎絎竴涓?
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}

]]>