![](/img/trans.png)
[英]Storing an arraylist in an object that is a part of that arraylist
[英]Storing a reference to the object in ArrayList
大家好,我正在嘗試將當前引用存儲到arraylist“ pl”。 例如pl.add(this); 由於某種原因,我僅獲得對最后一項的引用,而沒有任何先前的引用。 循環確實通過了所有三個項目。
下面是代碼,並把我得到。 誰能告訴我我在做什么錯,謝謝您的幫助。
// variables
private String productType;
private String hyperLinkParam;
private ArrayList <ProductList> pl = new ArrayList<ProductList> ();
public ProductList() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream url = null;
url = getClass().getResourceAsStream("inventory.xml");
Document doc = db.parse(url);
doc.getDocumentElement().normalize();
// loop through each item
NodeList items = doc.getElementsByTagName("item"); //Returns a list of elements with the given tag name item
for (int i = 0; i < items.getLength(); i++)
{
Element e = (Element) items.item(i);
setHyperLinkParam(e.getAttribute("name").toString());
setProductType(getTextValue(e,"productType"));
System.out.print(e.getAttribute("name").toString());
System.out.println(getTextValue(e,"productType"));
pl.add(this);
}
for(int j=0; j < pl.size(); j++){
System.out.print("getHyperLinkParam: " + pl.get(j).getHyperLinkParam());
System.out.println("getProductType: " + pl.get(j).getProductType());
}
制造.java
@WebMethod(operationName = "getProductList")
public ProductList getProductList() {
try {
ProductList productlist = new ProductList();
if(productlist == null){
return null;
}else{
return productlist;
}
} catch(Exception e){
System.out.println("error: " + e.getMessage());
return null;
}
}
index.jsp
<%
try {
org.soen487.supplychain.manufacturer.Manufacture_Service service = new org.soen487.supplychain.manufacturer.Manufacture_Service();
org.soen487.supplychain.manufacturer.Manufacture port = service.getManufacturePort();
// TODO process result here
org.soen487.supplychain.manufacturer.ProductList result = port.getProductList();
out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}
%>
這就是問題:
pl.add(this);
您一次又一次地添加相同的引用( this
)。 值this
僅僅是 “當前”對象的引用-你正在修改的循環,對象的內容。
在循環的每次迭代中,您應該創建一個新的單獨的對象,在該對象上設置屬性,然后將對該對象的引用添加到列表中。
這是稍微奇怪的是,你會加入this
在所有的名單,說實話-通常這樣的方法將是一些類,知道如何解析XML,而不是數據項本身的一個實例。 不清楚在哪里聲明了pl
,但是您應該真正考慮程序的結構。
您繼續將相同的對象(此)添加到列表中。 盡管您更改了成員,但列表中的所有引用仍然引用同一對象。
您應該創建一個新對象並添加它。
看到我的其他帖子
嘗試
ProductList obj=new ProductList();
// some work on this object and then store it in List
pl.add(obj);
它應該起作用,因為它將為您提供新鮮的物體
我試圖從這里發布的內容猜測出ProductList和Product的總體結構。 問題在於列表元素的列表和字段似乎在單個類(即ProductList)的上下文中。 這不會做-需要兩個類。
// stores the data coming from a single Element ("item") of the document
class Product {
private String productType;
private String hyperLinkParam;
public setHyperLinkParam( String hlp ){
hyperLinkParam = hlp;
}
public setProductType( String pt ){
productType = pt;
}
}
// Container for a list of products from an inventory
class ProductList {
private ArrayList <Product> pl = new ArrayList<Product> ();
public ProductList() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream url = getClass().getResourceAsStream("inventory.xml");
Document doc = db.parse(url);
doc.getDocumentElement().normalize();
// loop through each item
NodeList items = doc.getElementsByTagName("item");
for (int i = 0; i < items.getLength(); i++){
Element e = (Element) items.item(i);
// create the single product from the current item
Product prod = new Product();
prod.setHyperLinkParam( e.getAttribute("name").toString() );
prod.setProductType( getTextValue( e, "productType") );
// add it to the list
pl.add( prod );
}
}
void showList(){
for( Product prod: pl ){
System.out.print("getHyperLinkParam: " + prod.getHyperLinkParam());
System.out.println("getProductType: " + prod.getProductType());
}
}
}
注意:如果使用工廠方法makeProductList和makeProduct將ProductList和產品的構造放在Factory類中,則一切將變得更加清晰。 一個ProductList應該有一個addProduct方法,將add委托給它的pl成員。 而且關於如何從XML獲取產品列表的知識應該存在,而不是在構造函數中,並且類似地,從“ item”元素獲取Product的字段值的方式也不屬於產品或產品列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.