Java实例大全
方法一:使用commons包中ArrayUtils.addAll(Object[], Object[])
jar包地址:https://mvnrepository.com/artifact/org.apache.commons/commons-lang3/3.7
@Test
public void test6(){
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
int[] combinedIntArray = ArrayUtils.addAll(intArray,intArray2);
System.out.println(Arrays.toString(combinedIntArray));
}
方法二:System.arraycopy()
public static void arraycopy(Object src,int srcPos,Object dest,int destPos,int length)
其中:src表示源数组,srcPos表示源数组要复制的起始位置,desc表示目标数组,destPos表示目标数组被粘贴的起始位置,length表示要复制的长度。
@Test
public void test6(){
int[] intArray = { 1, 2, 3, 4, 5 };
int[] intArray2 = { 6, 7, 8, 9, 10 };
int[] newArray = new int[intArray.length+intArray2.length];
System.arraycopy(intArray,0,newArray,0,intArray.length);
System.arraycopy(intArray2,0,newArray,intArray.length,intArray2.length);
System.out.println(Arrays.toString(newArray));
}