Хранение полного массива в узле - PullRequest
0 голосов
/ 07 мая 2011

Я пытаюсь сохранить массив целых в узле. Я не получаю узел так, как должен. Любая помощь, которую кто-либо может оказать, будет великолепна.

  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(); 

Ответы [ 2 ]

0 голосов
/ 07 мая 2011

Я не уверен, в чем ваша проблема на самом деле (что, по вашему мнению, должно получить), но это может быть так:

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 голосов
/ 07 мая 2011

Если у вас нет большого предварительно выделенного массива, я считаю, что вам нужно выделить новый массив перед его заполнением.

Вы также можете не забыть увеличить num.

...