好的,所以我试图比较通用数组列表中的引用。
for (int s=0; s<orders.size(); s++) {
if (orders.get(s).contains(cpCollection.get(m)))
System.out.println(orders.get(s).getSerial());
}
我收到一条错误消息,它找不到contains方法的符号。
我想我需要指出的是,它不是普通的arraylist。这是更多代码。
订货是这个
ArrayList<GenericOrder<Product>> orders = new ArrayList<GenericOrder<Product>>();
类名是GenericOrder。这是文件开头的代码,用于清除其他内容。
public class GenericOrder<T> {
private ArrayList<T> products;
public GenericOrder() {
products = new ArrayList<T>();
}
}
因此,基本上我需要比较数组引用,以便检查作为对象的产品是否位于订单arraylist(即GenericOrder arraylist)内。
如果contains返回true,那么我要打印该订单的序列号,这是一种getSerial方法。如果我要说orders.get(1).getSerial(),则此方法可以正常工作。这将返回“ Order:1”。
我希望这是有道理的。如果需要,我可以发布整个代码,但是我想避免这种情况,因为它是大学的功课,所以我不希望有人偷走整个代码。
非常感谢。
最佳答案
正如MAKKAM在评论中所说,这是失败的:
GenericOrder<Product> order = orders.get(s);
order.contains( ... ) // Doesn't compile
GenericOrder类没有contains()方法。
您的两种解决方案是公开内部列表:
order.getProducts().contains( ... ); // Will work
或实现一个contains()方法:
public class GenericOrder<T> {
private ArrayList<T> products;
public boolean contains(T t) {
return products.contains(t);
}
}
关于java - 使用包含来比较通用数组列表中的引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8131686/