在一个节点中存储一个完整的数组
我试图在一个节点中存储一个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();
除非你有一个大的预分配数组,否则我相信你需要在填充它之前分配一个新数组。您可能还想记得增加num
。
谢谢。分配新阵列的想法解决了我的问题。 – 2011-05-07 13:23:55
我明白了。很高兴看到你修好了:) – aioobe 2011-05-07 13:30:44
我不知道你的问题还真是(应该您在您看来会得到什么?),但它可能是这样的:
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);
谢谢。分配新阵列的想法解决了我的问题。 – 2011-05-07 13:23:12
究竟什么是你的问题? – 2011-05-07 07:05:29