对象创建语句放在循环外和循环内的区别
2017-08-01 16:48
218 查看
每一个对象创建的时候都会有一个ID。List对象在执行add方法的时候是将对象的引用放入List中。上述代码中创建对象的语句如果放在外面,在将对象add到List中时,前后向List中add的对象都是同一个,所以放在外面的时候最后List中的对象是同一个对象。
@Override public List<EasyUITreeNode> getItemCatList(long parentId) { TbItemCatExample example = new TbItemCatExample(); Criteria criteria = example.createCriteria(); criteria.andParentIdEqualTo(parentId); List<TbItemCat> list = mapper.selectByExample(example); List<EasyUITreeNode> resultList = new ArrayList<>(); for (TbItemCat itemCat: list) { EasyUITreeNode node = new EasyUITreeNode(); node.setId(itemCat.getId()); node.setText(itemCat.getName()); node.setState(itemCat.getIsParent() ? "closed" : "open"); resultList.add(node); for (int i = 0; i < resultList.size(); i++) { System.out.println(resultList.get(i).getText() }
相关文章推荐
- 创建String对象时, 使用String s=new String ("abc")和String s="abc"语句有什么区别?
- 第2章 Java编程基础——FAQ2.22 创建String对象时, 使用String s=new String (“abc”)和String s=“abc”语句有什么区别?
- 用new方法生成一个img对象和document.createElement方法创建一个img对象的区别
- C++用new和不用new创建类对象区别
- 老男孩教育每日一题-第63天-批量创建用户并设置随机密码(要求不能使用shell循环语句)
- 复杂的表达式放在循环语句里会报错
- java中new一个对象放在循环体里面与外面的区别
- C++中创建对象的时候加括号和不加括号的区别
- 使用MyBatis Generator自动创建表对应的dao层接口、模型层对象、sql语句的xml文件
- python循环语句与C语言的区别
- java语言基础-控制循环语句-break- continue-return的区别
- C++ 堆上创建对象与栈上创建对象的区别 创建对象时有和无花括号的区别
- 声明对象和创建对象的区别
- C++用new和不用new创建类对象区别
- JAVA语言中创建对象后,调用对象中的方法,则方法中的输出语句会自动运行
- continue在循环语句中的区别
- Ruby on Rails,使用new和create创建ActiveRecord对象及他们之间的区别
- Java中string 创建对象时 “”和null的区别
- Python:continue和break跳出循环语句区别
- 浅谈JAVA中几种循环语句的区别