java - for循环中创建对象
迷茫
迷茫 2017-04-17 17:42:37
0
11
1599

下面这两种for循环中新建对象写法有什么区别呢?
第一种:

list<User> users = new ArrayList<User>();
User user = null;
for (int i = 0; i < 10; i++) {
    user = new User();
    user.setUserId(i);
    user.setUserName("segment" + i);
    users.add(user);
}
……

第二种:

list<User> users = new ArrayList<User>();
for (int i = 0; i < 10; i++) {
    User user = new User();
    user.setUserId(i);
    user.setUserName("segment" + i);
    users.add(user);
}
……

这两种写法对集合users中的内容有影响吗,还是只是性能上有区别?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(11)
迷茫

There is a difference between these two, and the execution results are different
Please refer to the following article:
http://ocaicai.iteye.com/blog...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template