将对对象的引用存储在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
}
%>
异想天开

我试图从这里发布的内容猜测出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的字段值的方式也不属于的代码。产品或产品列表。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将对对象属性的引用存储在变量中

来自分类Dev

将对对象属性的引用存储在变量中

来自分类Dev

在类中存储对对象的const引用

来自分类Dev

在TeeChart中存储对对象的引用

来自分类Dev

在字典中存储对对象的引用

来自分类Dev

PHP将对象引用存储在数组中

来自分类Dev

将对象引用存储到易失性字段中

来自分类Dev

Firemonkey TTreeView-将对象引用存储在TTreeViewItems,TValue中

来自分类Dev

是否应该重写哈希码以将对象存储在ArrayList中?

来自分类Dev

我们如何在指针中存储对对象的引用?

来自分类Dev

存储对象的值,而不是对对象本身的引用

来自分类Dev

在没有反射的情况下将对对象字段的引用传递并存储到另一个对象

来自分类Dev

在Java中对对象的双重引用

来自分类Dev

在Java中对对象的双重引用

来自分类Dev

C#中的对象实例化之间的区别:将对象存储在引用中与直接调用方法

来自分类Dev

在ArrayList中对对象进行排序

来自分类Dev

在ArrayList中对对象进行排序

来自分类Dev

将对象存储在localStorage中

来自分类Dev

Java:将对象类型的 Array 和 ArrayList 存储在 ArrayList 中(作为参数传递给方法)

来自分类Dev

在不丢失对对象的引用的情况下将对象转换为子类对象

来自分类Dev

对对象的“可写”引用

来自分类Dev

创建对对象的引用

来自分类Dev

如何将对象的引用存储在另一个类 C++ 中?

来自分类Dev

使用另一个类将对象存储在ArrayList中

来自分类Dev

一个应该重写哈希码以将对象存储在ArrayList中吗?

来自分类Dev

在c ++中,删除对象时清除对对象的引用

来自分类Dev

如何在Java中将对对象的引用作为参数返回

来自分类Dev

将对象附加到本地存储中

来自分类Dev

将对象存储在会话变量中

Related 相关文章

  1. 1

    将对对象属性的引用存储在变量中

  2. 2

    将对对象属性的引用存储在变量中

  3. 3

    在类中存储对对象的const引用

  4. 4

    在TeeChart中存储对对象的引用

  5. 5

    在字典中存储对对象的引用

  6. 6

    PHP将对象引用存储在数组中

  7. 7

    将对象引用存储到易失性字段中

  8. 8

    Firemonkey TTreeView-将对象引用存储在TTreeViewItems,TValue中

  9. 9

    是否应该重写哈希码以将对象存储在ArrayList中?

  10. 10

    我们如何在指针中存储对对象的引用?

  11. 11

    存储对象的值,而不是对对象本身的引用

  12. 12

    在没有反射的情况下将对对象字段的引用传递并存储到另一个对象

  13. 13

    在Java中对对象的双重引用

  14. 14

    在Java中对对象的双重引用

  15. 15

    C#中的对象实例化之间的区别:将对象存储在引用中与直接调用方法

  16. 16

    在ArrayList中对对象进行排序

  17. 17

    在ArrayList中对对象进行排序

  18. 18

    将对象存储在localStorage中

  19. 19

    Java:将对象类型的 Array 和 ArrayList 存储在 ArrayList 中(作为参数传递给方法)

  20. 20

    在不丢失对对象的引用的情况下将对象转换为子类对象

  21. 21

    对对象的“可写”引用

  22. 22

    创建对对象的引用

  23. 23

    如何将对象的引用存储在另一个类 C++ 中?

  24. 24

    使用另一个类将对象存储在ArrayList中

  25. 25

    一个应该重写哈希码以将对象存储在ArrayList中吗?

  26. 26

    在c ++中,删除对象时清除对对象的引用

  27. 27

    如何在Java中将对对象的引用作为参数返回

  28. 28

    将对象附加到本地存储中

  29. 29

    将对象存储在会话变量中

热门标签

归档