我正在尝试在节点中存储一个整数数组。我没有按照我应该的方式获得节点。任何人都可以提供的任何帮助都会很棒。
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();
除非你有一个大的、预分配的数组,否则我相信你需要在填充它之前分配一个新数组。
您可能还想记住递增num
。
我不确定你的问题到底是什么(你认为应该得到什么?),但它可能是这样的:
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);