1、使用 System.arraycopy()函数拷贝数组
public class ArrayCopyDemo { public static void main(String[] args) { int Arr1[] = new int[] { 1, 2, 3, 4, 5 }; int Arr2[] = new int[] { 9, 8, 7, 6, 5, 4, 3 }; System.arraycopy(Arr1, 0, Arr2, 0, 3);//static类型方法,直接类名调用。 // 复制源数组中从下标 0 开始的 3 个元素到目的数组,从下标 0 的位置开始存储。 for (int i = 0; i < Arr1.length; i++) System.out.print(Arr1[i]); System.out.println(); for (int j = 0; j < Arr2.length; j++) System.out.print(Arr2[j]); System.out.println();//复制的数组元素的个数一定不要超过目的数组的长度。 } }
2、 Arrays.sort 来对数组排序
package SortDemo; import java.util.*; public class SortDemo { public static void main(String[] args) { int Arr[] = new int[] { 10, 21, 4, 9, 8, 3, 34, 99 }; Arrays.sort(Arr); for (int i = 0; i < Arr.length; i++) { System.out.print(Arr[i]); System.out.print("\t"); } } }