Topic: 把自己构造的类加入到ArrayList,如何取回结果?

  Print this page

1.把自己构造的类加入到ArrayList,如何取回结果? Copy to clipboard
Posted by: jimmyofth
Posted on: 2008-01-10 16:37

自己构造一个类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

2.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: jimmyofth
Posted on: 2008-01-10 16:45

结果好像是方法里最后存储在mo里的信息。
如果在最后一个al.add()后再加两句
    mo.setItemCode("004");
    mo.setItemName("four");
但并没有add()
那么结果就变成
0:004---four
1:004---four
2:004---four

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

3.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: zcjl
Posted on: 2008-01-10 17:01


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


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

4.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: jimmyofth
Posted on: 2008-01-10 17:20

非常感谢。
看来ArrayList.add()并不是复制对象,而是引用。

学艺不精,见笑!

5.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: dylyonia
Posted on: 2008-01-15 14:39

你把对象放进去后,又改变了它的属性,相当于最后所有的对象的属性相同。

6.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-01-16 22:27

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

7.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: jimmyofth
Posted on: 2008-01-18 11:20

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());
}

}

8.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: jimmyofth
Posted on: 2008-01-18 12:08

这段问题出在哪(除了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;


}

9.Re:把自己构造的类加入到ArrayList,如何取回结果? [Re: jimmyofth] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2008-01-18 18:05

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()

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

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

嘉帆


   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