Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-01-16 22:27 user profilesend a private message to usersend email to JiafanZhousearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
First of all, your code looks very very bad-written.


ArrayList al=new ArrayList(); // why do your instantiate an instance here?
MyObject mo=new MyObject(); // why do your instantiate an instance here?
TestArrayList tal=new TestArrayList();
al=tal.getObjectList();


solution: remove the unnecessary newMoon.


// Do you realise that every time this method being invoked, your created two more instance? They will hit the performance bottleneck one day.
public ArrayList getObjectList(){
ArrayList al=new ArrayList();
MyObject mo=new MyObject();
mo.setItemCode("001");
mo.setItemName("one");
al.add(mo);
mo.setItemCode("002");
mo.setItemName("two");
al.add(mo);
mo.setItemCode("003");
mo.setItemName("three");
al.add(mo);

return al;


}


solution: refactor this method is required.

And to answer your question, yes, your guess is right. Object references are added into the Arraylist, hence changing one will affect the others.

Regards,
Jiafan



When I was a kid I used to pray every night for a new bike. Then I realized that The Lord doesn't work that way, so I stole one and asked him to forgive me.

冒泡。。。

话题树型展开
人气 标题 作者 字数 发贴时间
9550 把自己构造的类加入到ArrayList,如何取回结果? jimmyofth 1654 2008-01-10 16:37
7840 Re:把自己构造的类加入到ArrayList,如何取回结果? jimmyofth 202 2008-01-10 16:45
7803 Re:把自己构造的类加入到ArrayList,如何取回结果? zcjl 459 2008-01-10 17:01
7807 Re:把自己构造的类加入到ArrayList,如何取回结果? jimmyofth 49 2008-01-10 17:20
7594 Re:把自己构造的类加入到ArrayList,如何取回结果? dylyonia 34 2008-01-15 14:39
7444 Re:把自己构造的类加入到ArrayList,如何取回结果? JiafanZhou 1081 2008-01-16 22:27
7614 Re:把自己构造的类加入到ArrayList,如何取回结果? jimmyofth 777 2008-01-18 11:20
7502 Re:把自己构造的类加入到ArrayList,如何取回结果? jimmyofth 602 2008-01-18 12:08
7603 Re:把自己构造的类加入到ArrayList,如何取回结果? JiafanZhou 352 2008-01-18 18:05

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2020 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号
客服电话 0592-8750026    客服信箱 714923@qq.com    客服QQ 714923