【发布时间】:2020-11-05 22:44:10
【问题描述】:
我试图将一个文件读入一个数组,然后将该数组读入一个对象类,但它似乎只多次读取第一行。
String[] clothesFile = null;
Clothes[] clothes = new Clothes[2000];
br = new BufferedReader(new FileReader("clothes.csv"));
while ((line = br.readLine()) != null) {
assets = line.split(",");
name = clothes[0];
style = clothes[1];
colour = clothes[2];
brand = clothes[3];
}
for (int ii = 0; ii < n; ii++) {
clothes[ii] = new Clothes(name, style, colour, brand);
}
System.out.println("Clothes: ");
for (int ii = 0; ii < 10; ii++) {
System.out.print(ii + 1 + ". ");
System.out.print(clothes[ii]);
}
当我打印出衣服数组时,它只打印第一行 10 次,所以我假设它已经正确地创建了一个新对象。这与我混淆的循环有关吗?谢谢
【问题讨论】:
-
您需要将衣服添加到您的阵列中,因为它正在穿过线条,而不是之后。您目前只是覆盖名称、样式、颜色和品牌变量,而不是在您遍历每一行之前存储它们,因此您只保留最后一个覆盖。
-
我认为
name = clothes[0];应该是name = assets[0]; -
@MNEMO 哎呀,我的真实代码是资产,它用于 16 个不同的变量,但我不想发布原始代码/它太长了哈哈一定错过了那个