我正在实现一个LinkedListDeque
的数据 struct .我已经编写了一个equals
方法和一个帮助器:
- 判断对象
o
是否为类LinkedListDeque
的实例.如果是真的, - 判断
o
中的每个 node 是否等于this
双队列中的 node .
我编写了一个帮助器,因为我需要一种方法来在2
中对两个LinkedListDeque
对象进行比较操作,而不是在this
双队列和o
object
之间进行比较.当我第一次编写equals_helper方法时,我省略了类型声明中的圆括号.测试过了,不起作用.IDE建议我用圆括号将类型声明括起来(即equals_helper(LinkedListDeque o)
到equals_helper((LinkedListDeque) o)
),that起作用了.
我试图找到一些关于我在这里做什么的文档,但我没有背景知识来知道我在寻找什么.What is this concept called, am I applying it correctly in the context of Java OOP principles, and is there a better way to do this?个
public boolean equals(Object o) {
if (!(o instanceof LinkedListDeque)) {
return false;
} else {
return this.equals_helper((LinkedListDeque) o);
}
}
public boolean equals_helper(LinkedListDeque L) {
if (L.size() != this.size()) {
return false;
} else {
Node orig_curr = sentinel.next;
Node curr = L.sentinel.next;
while (orig_curr.item != null || curr.item != null) {
if (orig_curr.item == curr.item) {
curr = curr.next;
orig_curr = orig_curr.next;
} else return curr.next.item == null && orig_curr.next.item == null;
}
}
return true;
}