2016-11-26 33 views
0

所以我有这样的代码:链接列表中的多维数组无法正常工作?

public static void main (String[] args) throws IOException 
{ 
    Queue popcorn = new LinkedList(); 
    BufferedReader in = null; 
    int j = 0; 
    try { 
     File file2 = new File("Events.txt"); 
     in = new BufferedReader(new FileReader(file2)); 

     String str; 
     String [][] process = new String[][]; 
     while ((str = in.readLine()) != null) { 
      String[] arr = str.split(" "); 
      for(int i=0 ; i<str.length() ; i++){ 
      process[j][i] = in.readLine(); 
     } 
      j++; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      in.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

它不工作。它抛出“变量必须提供尺寸表达式或数组初始化 ”

我想这个答案的网页后,其建模“http://www.chegg.com/homework-help/questions-and-answers/hired-biggy-s-popcorn-handle-popcorn-orders-store-write-java-console-application-reads-dat-q7220573” 这我敢肯定是行不通的。无论如何,这个链表似乎没有解决。就我的String [] []过程声明而言,有人能指出我正确的方向吗?

+0

'新的String [] []' - 这是不可能的* *创建无维数组,这是信息说什么。这与变量声明是分开的。搜索关于一般提示/方向的错误消息。 – user2864740

+0

您需要提供数组大小。阅读http://www.java67.com/2014/10/how-to-create-and-initialize-two-dimensional-array-java-example.html – Rohan

回答

3

你不能只是初始化一个没有维度参数的数组。例如,这是无效的:

int[] array = new int[]; 

它需要像这样进行初始化:

int[] array = new int[10]; 

或者干脆:

int[] array; 
// ... // 
array = new int[10]; 

这是一个与多维数组同样的事情。为了使含5号的3个数组的数组,你会放:

int[][] array = new int[3][5]; 

然而,随着二维数组,你也可以把:

int[][] array = new int[3][]; 
array[0] = new int[5]; 
array[1] = new int[7]; 
// ... // 

的一点是,你需要定义多少其他数组将在您的基本数组中,并且还可以选择定义所有数组的大小或稍后添加它们。在这种情况下,你需要改变

String [][] process = new String[][]; 

喜欢的东西

String [][] process = new String[x][y]; 
+0

好吧!所以我会用String [] [] process = new String [15] [];对不对? – user7212219

+0

是的,但请记住,如果您要使用'process [j] [i] = in.readLine();'行,您可能需要使用其他数组填充基本数组通过设置'process [j] = arr;'。 –