一维数组既可以在定义时候初始化(例1),又可以在定义之后初始化(例2)。

例1:

 
  
  1. public static void main(String[] args) {  
  2.     int array[] = new int[]{100,200};  
  3. }  

 

例2:

 
  
  1. public static void main(String[] args) {  
  2.     int array[] = new int[2];  
  3.     array[0]=100;  
  4.     array[1]=200;  

而二维数组只能在定义时候初始化(例3),不可以在定义之后初始化,定义之后初始化会报错(图1)。

例3:

 
  
  1. public static void main(String[] args) {  
  2.     int grade[][] =   
  3.             new int[][]{{1,2,3,4,5},{6,7,8,9,10}};  

图1:

 

上面这段代码不能编译通过,会提示错误:Array constants can only be used in initializers。