【发布时间】:2016-04-24 06:27:08
【问题描述】:
在 2 天内,我有一个编码任务到期,但我根本无法为我的任务找到解决方案。
任务:之前我们已经编写了自己的链接数据结构。现在我们应该实现泛型来在这个结构中保存不同的对象(在我们的例子中是银行,有账户、账户持有人、注册表)。
我们不允许使用 java.util 中的任何方法。我们只允许使用 java.io 和 java.lang。
我很难找到一种方法来搜索我的列表。
例如这段代码:
class Account {
int accountNumber;
int bankCode;
int balance;
Account(int bankCode, int accountNumber) {
this.bankCode = bankCode;
this.accountNumber = accountNumber;
this.balance = 1000;
}
我想通过以 int accountNumber 作为参数进行搜索来查找帐户。 我尝试使用 foreach 循环,但是对于我的自定义列表它不适用。 这是自定义列表的基本设计:
class List<L> {
ListCell<L> first = null;
(methods like add ... )
class ListCell<L> {
ListCell<L> next = null;
L content = null;
ListCell(L content, ListCell<L> next) {
this.content = content;
this.next = next;
}
}
我试过了,但得到了上面提到的错误:
<L> boolean contains (L l, List<L> list) {
for (L m : list) if (m.equals(l)) return true;
return false;
}
【问题讨论】:
-
你遇到了什么错误?
标签: java list generics collections