deepEquals方法在我的ArrayDeque文件中有问题,但我找不到它。这对LinkedListArrayDeque也是有意义的。
如何不使用deepEquals 使工作
下面的代码是一个双结束数组队列,其中第一个项是在数组中间添加的。我删除了几种方法,以获得简短的视图。
package deque;
import java.util.Iterator;
public class ArrayDeque<T> implements Deque<T>, Iterable<T> {
private T[] ts;
private int size;
private int stposition;
private int firposition;
private int lastposition;
public ArrayDeque() {
ts = (T[]) new Object[8];
size = 0;
stposition = Math.round(ts.length / 2);
firposition = stposition;
lastposition = stposition;
}
public T get(int i) {
if (size < i | size == 0) {
return null;
}
int pos = (firposition + i) % ts.length;
return ts[pos];
}
public int size() {
return size;
}
@Override
public Iterator<T> iterator() {
return new ArrayDequeIterator();
}
private class ArrayDequeIterator implements Iterator<T> {
private int pos0 = firposition;
public boolean hasNext() {
if (size == 0) {
return false;
}
if (pos0 == lastposition) {
return true;
}
if (size > 1) {
if (firposition < lastposition) {
if (pos0 < lastposition) {
return true;
}
} else {
if (pos0 + 1 < ts.length) {
return true;
}
}
return false;
}
return false;
}
public T next() {
T x = ts[pos0];
pos0 = (pos0 + 1) % ts.length;
return x;
}
}
@Override
public boolean equals(Object o) { // the equal method passed the tests but deepequal fail
if (o == this) {
return true;
}
if (o == null || this == null) {
return false;
}
if (!(o instanceof Deque)) {
return false;
}
Deque oll = (Deque) o;
if (oll.size() != this.size()) {
return false;
}
for (int i = 0; i < this.size(); i++) {
Object a2 = oll.get(i);
Object a1 = this.get(i);
if (a1 == a2) {
continue;
}
if (a2 == null) {
return false;
}
if (a1.getClass() != a2.getClass()) {
return false;
}
return deepEquals(a1, a2);
}
return true;
}
private boolean deepEquals(Object a1, Object a2) {
boolean deq;
if (a1 instanceof Deque) {
// maybe it's wrong here, I am not sure how to write this
deq = a1.equals(a2);
} else {
if (a1 == a2) {
return true;
}
return false;
}
return deq;
}
},我终于弄明白了。谢谢你的帮助.
它实际上不需要另一个deepEqual方法。
equals方法本身就足够了。
守则如下:
(1 )我的deque接口中没有deque方法,所以我只使用了get(i)方法。但是我可以用它来做this。谢谢@knittl的建议。
(!a1.equals(a2))在我的代码中很重要。我终于想出来了!.).public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o == null) {
return false;
}
if (!(o instanceof Deque)) {
return false;
}
Deque oll = (Deque) o;
if (oll.size() != this.size()) {
return false;
}
int i = 0;
for (final Object a1 : this) {
Object a2 = oll.get(i);
i += 1;
if (a1 == a2) {
continue;
}
if (a2 == null) {
return false;
}
if (a1.getClass() != a2.getClass()) {
return false;
}
if (!a1.equals(a2)) {
return false;
}
}
return true;
}发布于 2022-11-27 11:08:36
您将希望您的equals方法比较列表中的每个项是否相等。如果两项不相等,则返回false。请注意,通过索引访问链接列表中的项是O(n),这意味着您的等价物方法具有二次运行时复杂性。使用迭代器来避免这种情况。
// ...
for (int i = 0; i < this.size(); i++) {
Object a2 = oll.get(i);
Object a1 = this.get(i);
if (!Objects.equals(a1, a2)) {
return false;
}
}
return true;
}使用迭代器(这使您具有线性运行时复杂性):
// ...
Iterator<Object> otherIterator = oll.iterator();
for (final Object a1 : this) {
// guaranteed to work, because both lists have the same size:
final Object a2 = otherIterator.next();
if (!Objects.equals(a1, a2)) {
return false;
}
}
return true;
}https://stackoverflow.com/questions/74589117
复制相似问题