一维数组:
int[] a;二维数组:
int[][] a;数组的初始化方式有以下几种:
特殊初始化: 这种初始化不使用new关键字,在数组声明的同时完成初始化操作,也被称为静态初始化,主要原因是因为采用这种初始化的方式,数组的存储空间的分配是由编译器完成的: int[] a={1,2,3};使用new关键字创建数组,同时为数组中的元素赋值: 在这种用法下new不需要指定数组的长度:int[] a=new int[]{1,2,3};将数组全部初始化为特定的值: boolean[] test=new boolean[n]; Arrays.fill(test,true);使用System.arraycopy System.arraycopy(源数组名称,源数组开始点,目标数组名称,目标数组开始点,拷贝长度) 将目标数组中的一部分替换成源数组中的一部分
public class test { public static void main(String[] args){ int[] arr=new int[]{1,2,3,4,5}; int[] temp=new int[]{6,7,8,9,10}; System.arraycopy(temp,0,arr,0,temp.length); for(int i=0;i<arr.length;i++){ System.out.print(arr[i]+" "); } } } 输出: 6 7 8 9 10