动态初始化:
int[]arr1 = new int[5]; //声明、创建
arr1[0] = 0;//初始化
arr1[1] = 1;
arr1[2] = 2;
arr1[3] = 3;
arr1[4] = 4;
静态初始化:
形式1:
int[] arr2 = new int[] {1,2,3,4,5};//声明、创建、初始化
错误写法:
int[] arr2 = new int[5]{1,2,3,4,5};
形式2:
int[]arr2 ;//声明数组标识符
arr2= new int[] {1,2,3,4,5};//创建数组对象,并初始化
错误写法:
int[]arr2 ;
arr2= new int[5] {1,2,3,4,5};
形式3:用{}直接初始化
int[] arr2 = {1,2,3,4,5};
错误写法:
int[] arr2;
arr2 = {1,2,3,4,5};
如果想要用{}直接初始化,那么声明和初始化必须在一起