PS(首先需要导入commons-lang.jar,我的是3-3.6版本
package com.zyy.common; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.ArrayUtils; /** * ArrayUtils常用方法除了下面的,还有isEmpty(),add(),indexOf() * ,isEquals()等 * @author ZYY * */ public class TestArrayUtils { public static void main(String []args) { testToObject(); } /** * Contains * 如果某个数组包含某个值就返回true * 否则返回false */ public static void testContains() { int []array= {1,2,3}; System.out.println(ArrayUtils.contains(array, 2));//true System.out.println(ArrayUtils.contains(array, 4));//false } /** * AddAll * 把另一个数组的值全部赋给 * 另一个数组,创建出一个新的数组,原数组的值不变 */ public static void testAddAll() { int []array1= {1,2,3}; int []array2= {4,5,6}; int[] array3= ArrayUtils.addAll(array1, array2); for (int i : array3) { System.out.println(i); } } /** * 复制对象 */ public static void testClone() { int []array1= {1,2,3}; int[] clone = ArrayUtils.clone(array1); for (int i : clone) { System.out.println(i); } } /** * 截取某个数组的值 * 从startIndexInclusive * 到endIndexExclusive * 即[) */ public static void testSubarray() { int []array= {4,5,6}; int[] subarray = ArrayUtils.subarray(array,0, 2); for (int i : subarray) { System.out.println(i); } } /** * 将基本类型数组变成 * 对应的引用类型数组 */ public static void testToObject() { int []array= {4,5,6}; Integer[] array1 = ArrayUtils.toObject(array); for (Integer integer : array1) { System.out.println(integer); } } }
3-3.6版本)