2011-05-07 72 views
0

我試圖在一個節點中存儲一個int數組。我沒有按照我應該的方式獲得節點。任何人可以提供的幫助都會很棒。在一個節點中存儲一個完整的數組

public void readIn() 
    { 
    int counter = 1; 
    try { 
     Scanner inFile = new Scanner(new FileReader("WordProblemData.txt")); 
     int times = Integer.parseInt(inFile.next()); 
     for (int a = 1;a <= times; a++) 
     { 
      for (int i = 1; i <= 8; i++) 
      { 
       num[i-1] = Integer.parseInt(inFile.next()); 
       System.out.println(num[i-1]); 

      } 
      data = (String)(inFile.next()); 
      System.out.println(data); 



      head = new DateStampNode(data,num,head); 
     } 
     inFile.close(); 
+0

究竟什麼是你的問題? – 2011-05-07 07:05:29

回答

0

除非你有一個大的預分配數組,否則我相信你需要在填充它之前分配一個新數組。您可能還想記得增加num

+0

謝謝。分配新陣列的想法解決了我的問題。 – 2011-05-07 13:23:55

+0

我明白了。很高興看到你修好了:) – aioobe 2011-05-07 13:30:44

0

我不知道你的問題還真是(應該您在您看來會得到什麼?),但它可能是這樣的:

int[] num = new int[8]; //you should allocate a new array, otherwise you'd overwrite the values in the next iteration of the outer loop 
for (int i = 1; i <= 8; i++) 
{ 
    num[i-1] = Integer.parseInt(inFile.next()); 
    System.out.println(num[i-1]); 
} 
data = (String)(inFile.next()); 
System.out.println(data); 

//you're storing num here, so you'd need a new num array for the next iteration, see above 
head = new DateStampNode(data,num,head); 
+0

謝謝。分配新陣列的想法解決了我的問題。 – 2011-05-07 13:23:12

相關問題