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

您没有登录

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

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





发贴: 18
积分: 0
于 2008-01-10 16:37 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
自己构造一个类MyObject,在另一个类里构造一个方法,返回结果为ArrayList。
在方法里把MyObject数据放入ArrayList,调用该方法想取回数据,结果无法取回,请各位指教。

源代码如下:
MyObject.java

package myTest;

public class MyObject {
private String itemCode;
private String itemName;

public String getItemCode() {
return itemCode;
}
public void setItemCode(String itemCode) {
this.itemCode = itemCode;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}

public MyObject(){

}

}

TestArrayList.java
/**
*
*/

package myTest;

import java.util.ArrayList;

public class TestArrayList {

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;


}

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList al=new ArrayList();
MyObject mo=new MyObject();
TestArrayList tal=new TestArrayList();
al=tal.getObjectList();
for(int i=0;i<al.size();i++)
{
mo=(MyObject) al.get( i );
System.out.print( i );
System.out.print(":");
System.out.print(mo.getItemCode());
System.out.print("---");
System.out.println(mo.getItemName());
}

}

}

运行结果
0:003---three
1:003---three
2:003---three


jimmyofth edited on 2008-01-18 12:13

作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
jimmyofth





发贴: 18
积分: 0
于 2008-01-10 16:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
结果好像是方法里最后存储在mo里的信息。
如果在最后一个al.add()后再加两句
    mo.setItemCode("004");
    mo.setItemName("four");
但并没有add()
那么结果就变成
0:004---four
1:004---four
2:004---four

而且,如果往ArrayList里添加的不是自定义对象,结果就是正常的。
为什么?



作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
zcjl

涅槃



发贴: 537
积分: 65
于 2008-01-10 17:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

public ArrayList getObjectList(){
ArrayList al=new ArrayList();

MyObject mo=new MyObject();
mo.setItemCode("001");
mo.setItemName("one");
al.add(mo);
mo=new MyObject();
mo.setItemCode("002");
mo.setItemName("two");
al.add(mo);
mo=new MyObject();
mo.setItemCode("003");
mo.setItemName("three");
al.add(mo);

return al;
}


改成上面的代码再试试 ^_^
然后根据前后的差异,自己想想问题可能出在哪里吧



作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
jimmyofth





发贴: 18
积分: 0
于 2008-01-10 17:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
非常感谢。
看来ArrayList.add()并不是复制对象,而是引用。

学艺不精,见笑!



作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
dylyonia





发贴: 2
积分: 0
于 2008-01-15 14:39 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你把对象放进去后,又改变了它的属性,相当于最后所有的对象的属性相同。


作者 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.
作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
jimmyofth





发贴: 18
积分: 0
于 2008-01-18 11:20 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Should I correct the problem like follows?

old version
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();


new version
public static void main(String[] args) {
// TODO Auto-generated method stub
TestArrayList tal=new TestArrayList();
ArrayList al =tal.getObjectList();
MyObject mo;

for(int i=0;i<al.size();i++)
{
mo=(MyObject) al.get( i );
System.out.print( i );
System.out.print(":");
System.out.print(mo.getItemCode());
System.out.print("---");
System.out.println(mo.getItemName());
}

}


jimmyofth edited on 2008-01-18 12:03

作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
jimmyofth





发贴: 18
积分: 0
于 2008-01-18 12:08 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
这段问题出在哪(除了arraylist的引用这个问题外)?refactor this method is required.是啥意思?
瓶颈会在哪?


// 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;


}


jimmyofth edited on 2008-01-18 12:12

作者 Re:把自己构造的类加入到ArrayList,如何取回结果? [Re:jimmyofth]
JiafanZhou



版主


发贴: 736
积分: 61
于 2008-01-18 18:05 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
Ok, your main method looks much better than before. At least the garbage collection daemon will not try to collect your first unused instance, which can greatly improve the performance.

"Refactor this method" 是重构代码的意思。

我前面已经指出来了这个方法的问题,每一次被调用的时候都会有两个实例被创建
1) new ArrayList()
2) new MyObject()

为什么不用一个全局变量呢?

当你这个方法被反复调用的时候,你就会看到瓶颈了。

嘉帆



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.

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-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923