【发布时间】:2014-02-16 18:36:05
【问题描述】:
大家好,我正在尝试将当前引用存储到数组列表“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());
}
Manufacture.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
}
%>
【问题讨论】: