【问题标题】:Method only applicable on single Instance of Class instead of ArrayList方法仅适用于类的单个实例而不是 ArrayList
【发布时间】:2019-04-18 13:34:35
【问题描述】:

我有 items 类,其中包含 mProductType 等字段。

在这个类里面有如下方法:

public ArrayList<Item> getProductsByCategory(String category, ArrayList<Item> productList) {

    ArrayList<Item> returnList = new ArrayList<>();

    for (int i = 0; i < productList.size(); i++) {

        if (productList.get(i).getmProductType() == category) {
            returnList.add(productList.get(i));
        }

    }
    return returnList;
}

到目前为止一切顺利。在我的另一个类ProductsInCategoryFragment 中,我尝试调用这样的方法:

ArrayList<Item> products;

// ... filling variable "products" with content
products = fetcher.getItems();

// ... declaring the category String
String category = getArguments().getString("CATEGORY");


products.getProductsByCategory(category, products);

我可以从类项的实例调用该方法,但不能在该类的 ArrayList 上调用。最后一行代码给了我这个错误:

Error:(82, 17) error: cannot find symbol method getProductsByCategory(String,ArrayList<Item>)

我知道这可能是一个非常基本的问题,但我似乎无法掌握它。这是某种可见性问题吗?

【问题讨论】:

    标签: java arraylist methods


    【解决方案1】:

    有了这个签名和实现,它可能是一个static 函数,因为它不使用来自当前 (this) 对象的任何成员。 ArrayList&lt;Item&gt; products 是一个 ArrayList,而不是你的 Item 类型,这就是它没有你的方法的原因。另外,将Strings 与== 进行比较通常不起作用,请改用equals()
    所以:

    public static ArrayList<Item> getProductsByCategory(String category, ArrayList<Item> productList) {
    
        ArrayList<Item> returnList = new ArrayList<>();
    
        for (int i = 0; i < productList.size(); i++) {
    
            if (category.equals(productList.get(i).getmProductType())) {
                returnList.add(productList.get(i));
            }
    
        }
        return returnList;
    }
    

    用作:

    ArrayList<Item> filteredItems = Item.getProductsByCategory(category, products);
    

    (假设getProductsByCategory()Item 中的一个方法,但可能有一个更好的地方,可能在您的main() 附近,或者您打算使用它的任何地方)

    【讨论】:

      【解决方案2】:

      应该是items.getProductsByCategory(category, products); 你尝试在ArrayList 对象上调用getProducts... 方法。

      【讨论】:

        【解决方案3】:

        您可以创建自定义ItemList&lt;Item&gt; 类扩展ArrayList&lt;Item&gt;。在此类中,您可以添加getProductsByCategory 方法。然后将您的产品变量更改为ItemList&lt;Item&gt; products;

        class ItemList<Item> extends ArrayList<Item> {
        
          public ArrayList<Item> getProductsByCategory(String category) {
            ArrayList<Item> returnList = new ArrayList<>();
            for (int i = 0; i < this; i++) {
              if (this.get(i).getmProductType().equals(category)) {
                returnList.add(this.get(i));
              }
            }
            return returnList;
          }
        
        }
        

        或者在 Java 8 中,您可以简单地使用流来根据类别过滤列表,而不必实现自己的 getProductsByCategory 函数。

        List<Item> result = products.stream()
                                    .filter(p -> (p.getmProductType().equals(category)))
                                    .collect(Collectors.toList());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-06-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-31
          • 1970-01-01
          相关资源
          最近更新 更多