比较两个 deque,一个由 Array 实现,另一个由 LinkedList 实现,是否相等?

Comparing two deques, one implemented by an Array and another implemented with a LinkedList, for equality?

提问人:bobcob 提问时间:6/10/2023 最后编辑:bobcob 更新时间:6/10/2023 访问量:85

问:

我正在努力实现一个自定义的 Deque 接口。除了 equals 方法外,一切都在工作。每当我将代码提交给自动检查器时,它都会失败其中一项相等性测试并输出:“具有相同元素的 ArrayDeques 和 LinkedListDeques 应该相等。我无法弄清楚我做错了什么。

这些分别是我的 LinkedListDeque 和 ArrayDeque 类中的 equals 方法(包含我的自定义 deque 接口的包的一部分)。

    /** Returns whether the parameter o is equal to the Deque.
     * o is considered equal if it is a Deque and if it contains
     * the same contents (as governed by the generic T's equals method) in the same order. */
 
@Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (o == this) {
            return true;
        }
        if (!(o instanceof Deque)) {
            return false;
        }

        if (o instanceof LinkedListDeque) {
            LinkedListDeque<T> other = (LinkedListDeque<T>) o;
            if (this.size() != other.size()) {
                return false;
            }
            Iterator<T> dequeIt = this.iterator();
            Iterator<T> otherIt = other.iterator();
            while (dequeIt.hasNext()) {
                if (!dequeIt.next().equals(otherIt.next())) {
                    return false;
                }
            }
            return true;
        } else if (o instanceof ArrayDeque) {
            ArrayDeque<T> other = (ArrayDeque<T>) o;
            if (this.size() != other.size()) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIt = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIt.next())) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }
}

   @Override
    public boolean equals(Object o) {
        if (o == null) {
            return false;
        }
        if (this == o) {
            return true;
        }
        if (!(o instanceof Deque)) {
            return false;
        }
        if (o instanceof ArrayDeque) {
            ArrayDeque<T> other = (ArrayDeque<T>) o;
            if (other.size() != this.size) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIterator = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIterator.next())) {
                    return false;
                }
            }
            return true;
        } else if (o instanceof LinkedListDeque) {
            LinkedListDeque<T> other = (LinkedListDeque<T>) o;
            if (other.size() != this.size) {
                return false;
            }
            Iterator<T> thisIt = iterator();
            Iterator<T> otherIterator = other.iterator();
            while (thisIt.hasNext()) {
                if (!thisIt.next().equals(otherIterator.next())) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }


It passes my unit tests: 
 @Test
    public void randomizedInsertionAndDeletion(){
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        LinkedListDeque<Integer> deque2 = new LinkedListDeque<>();
        int N = 5000;
        for (int i =0; i<N; i++) {
            int operationNumber = StdRandom.uniform(0, 4);
            int randValue = StdRandom.uniform(0, 100);
            if (operationNumber == 0) {
                deque.addFirst(randValue);
                deque2.addFirst(randValue);
                assertEquals((Integer) randValue, deque.removeFirst());
                assertEquals((Integer) randValue, deque2.removeFirst());
            } else if (operationNumber == 1) {
                deque.addLast(randValue);
                deque2.addLast(randValue);
                assertEquals((Integer) randValue, deque.removeLast());
                assertEquals((Integer) randValue, deque2.removeLast());
            } else if (operationNumber == 2) {
                assertEquals(deque.get(0), deque.removeFirst());
                assertEquals(deque2.get(0), deque.removeFirst());
            } else if (operationNumber == 3) {
                assertEquals(deque.removeLast(), deque.get(deque.size()));
            } else if (operationNumber == 4) {
                boolean actual1 = deque.equals(deque2);
                boolean actual2 = deque2.equals(deque);
                assertEquals(true, actual1);
                assertEquals(true, actual2);
            }
        }
    }

    @Test
    public void equals() {
        ArrayDeque<Boolean> deque = new ArrayDeque<>();
        deque.addLast(true);
        deque.addFirst(false);
        deque.addFirst(false);
        deque.addFirst(true);

        LinkedListDeque<Boolean> deque2 = new LinkedListDeque<>();
        deque2.addLast(true);
        deque2.addFirst(false);
        deque2.addFirst(false);
        deque2.addFirst(false);

        assertEquals(false, deque2.equals(deque));
        assertEquals(false, deque.equals(deque2));
    }

感谢您的任何指导。

Java 数据结构 等于 相等

评论

0赞 Sweeper 6/10/2023
您已经展示了 3 个实现。哪个是哪个?equals
0赞 bobcob 6/10/2023
对不起。我删除了第三个。第一个是 LinkedListDeque,第二个是 ArrayDeque。

答: 暂无答案