2017-08-13 54 views
2

嗯,我想用这个方法来初始化多个阵列,这让我提到这个错误:任何解决方案Java的multilpe数组初始化

  int [] x,y,z=new int [10]; 
      System.out.println("The first element of x:"+x[0]); 
      System.out.println("The second element of y:"+y[1]); 
      System.out.println("The third element of z:"+z[2]);` 

和错误是:

ex1.java:47: error: variable x might not have been initialized 
    System.out.println("The first element of x:"+x[0]); 
               ^
ex1.java:48: error: variable y might not have been initialized 
    System.out.println("The second element of y:"+y[1]); 
               ^
2 errors 
+0

我打算假设您打算包含错误消息,但由于某种原因它没有出现,并且您即将编辑您的问题以包含它。 –

+0

是的,这是正确的。我已经添加了错误信息 – heartySteam

回答

3

int [] x,y,z=new int [10]; 

等同于:

int[] x; 
int[] y; 
int[] z=new int [10]; 

xy未初始化。

你应该初始化所有3个数组:

int[] x = new int [10]; 
int[] y = new int [10]; 
int[] z = new int [10]; 
1
int [] x,y,z=new int [10]; 

你只在这一行初始化z,不x也不y。您还需要为其他两个变量添加其他初始化。

int [] x=new int [10], y=new int [10], z=new int [10]; 
+0

这是最基本的方式。但问题是,假设我想在一个行设定多于10 1-d阵,然后什么事的代码? – heartySteam

+0

相信我,你真的不想在单行初始化超过10个阵列。 –

+0

但我想代码?如果有什么我的问题... – heartySteam