【发布时间】:2015-02-01 03:24:18
【问题描述】:
我在 java 中使用 Map List,但遇到了麻烦。我用:
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
ArrayList<Map<String,AttributeValue>> maps = new ArrayList<Map<String,AttributeValue>>();
我使用 CSVReader 读取文件并将值存储在 ListOfMap 中
CSVReader reader = new CSVReader(new FileReader("data1.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
item.clear();
item.put("Id", new AttributeValue().withN(nextLine[0]));
item.put("Name", new AttributeValue().withS(nextLine[1]));
System.out.println("Item:"+item); // I try printing item
maps.add(item);
}
结果是:
Item:{Id={N: 0,}, Name={S: goGOv,}}
Item:{Id={N: 1,}, Name={S: TBlGD,}}
Item:{Id={N: 2,}, Name={S: OtXuw,}}
...
Item:{Id={N: 999,}, Name={S: QAMzc,}}
Item:{Id={N: 1000,}, Name={S: PumAq,}}
但是当我从这个列表中打印一些元素时
System.out.println(" "+maps.get(i)); // I tried i from 0-1000
它总是只显示 1 个输出
{Id={N: 1000,}, Name={S: PumAq,}}
所以任何人都可以告诉我我哪里错了。 谢谢,
【问题讨论】: