java for 最后一个元素_Java列表仅返回最后一个元素

我有一个pojo,尝试将csv文件中的数据读取到列表中,然后将其打印出来。从文件读取工作正常,在读取/添加的瞬间,我可以看到正确的ID,但是一旦尝试将其全部打印回来,我只会得到列表的最后一个元素。以下是我正在尝试的:

public static void main(String[] args) throws IOException, ParseException{

Charset charset = Charset.forName("UTF-8");

File dir = new File("/Users/vinnar/eclipse_keplar/workspace/vinnar-pojo-projects/src/com/vinnar/pojo/csvfiles");

File file = null;

file = new File(dir.getCanonicalPath()

+ File.separator

+ "Teams.csv");

FileInputStream fis = new FileInputStream(file);

BufferedReader br = new BufferedReader(new InputStreamReader(fis, charset));

String line = null;

Team team = new Team();

List teams = new ArrayList();

String csvSeperator = ",";

while ((line=br.readLine()) != null){

String[] t = line.split(csvSeperator);

System.out.println("Team ID is: " + t[0]);

team.setId(Integer.parseInt(t[0]));

team.setName(t[1]);

team.setRank(Integer.parseInt(t[2]));

team.setLstUpdUser(t[3]);

DateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy", Locale.ENGLISH);

team.setLstUpdTime((Date) dateFormat.parse(t[4]));

teams.add(team);

}

br.close();

for(Team t1:teams){

System.out.println("Team info: " + t1.getId());

}

}

我从上面得到的输出是:

团队编号为:1

团队编号为:2

团队编号为:3

团队编号为:4

球队信息:4

球队信息:4

球队信息:4

球队信息:4

我想念什么..?为什么前三个元素会丢失..?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值