【发布时间】:2011-12-29 05:20:06
【问题描述】:
好的,我正在尝试比较通用数组列表中的引用。
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<GenericOrder<Product>> orders = new ArrayList<GenericOrder<Product>>();
类名是GenericOrder。这是文件开头的代码,用于清除其他内容。
public class GenericOrder<T> {
private ArrayList<T> products;
public GenericOrder() {
products = new ArrayList<T>();
}
}
所以基本上我需要比较数组引用,以便检查作为对象的产品是否位于订单数组列表内,订单数组列表是一个 GenericOrder 数组列表。 如果包含返回 true,那么我想打印该订单序列号,这是一个 getSerial 方法。如果我说 orders.get(1).getSerial(); 这个方法很好用。这将返回“订单:1”。
我希望这是有道理的。如果需要,我可以发布整个代码,但我想避免这种情况,因为它是大学作业,我不希望有人窃取整个代码。
非常感谢。
【问题讨论】:
-
编译器说 GenericOrder 没有 contains 方法。只需执行它。顺便说一句,cpCollection 是什么?
-
cpCollection 是另一种类型的数组列表。它包含属于 ComputerParts 的产品。 (计算机部件继承自产品)。
标签: java generics collections arraylist contains