Java ArrayDeque的使用

Java ArrayDeque的使用,第1张

Java ArrayDeque的使用

以下是测试用例。

    @Test
    public void testArrayDequePollFirst() {
        Deque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        deque.pollFirst();
        Assert.assertEquals(deque.size(), 3);
        Assert.assertArrayEquals(deque.toArray(), new String[]{"2", "a2", "a3"});
    }

    @Test
    public void testArrayDequePeek01() {
        Deque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        String tmp = deque.peek();
        Assert.assertEquals(tmp, "a3");
        Assert.assertEquals(deque.size(), 4);
        Assert.assertArrayEquals(deque.toArray(), new String[]{"1", "2", "a2", "a3"});
    }

    @Test
    public void testArrayDequePeek02() {
        Deque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        String tmp = deque.peekFirst();
        Assert.assertEquals(tmp, "1");
        Assert.assertEquals(deque.size(), 4);
        Assert.assertArrayEquals(deque.toArray(), new String[]{"1", "2", "a2", "a3"});
    }

    @Test
    public void testArrayDequePeek03() {
        Deque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        String tmp = deque.peekLast();
        Assert.assertEquals(tmp, "a3");
        Assert.assertEquals(deque.size(), 4);
        Assert.assertArrayEquals(deque.toArray(), new String[]{"1", "2", "a2", "a3"});
    }

    @Test
    public void testArrayDequePollLast() {
        Deque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        deque.pollLast();
        Assert.assertArrayEquals(deque.toArray(), new String[]{"1", "2", "a2"});
    }

    @Test
    public void testArrayDequePush() {
        ArrayDeque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        deque.push("a4");
        Assert.assertEquals("a4", deque.pollFirst());
    }

    @Test
    public void testArrayDequePush02() {
        ArrayDeque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        deque.push("a4");
        deque.pop();
        deque.pop();
        Assert.assertEquals("2", deque.pollFirst());
    }

    @Test
    public void testArrayDequePush03() {
        ArrayDeque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("a2");
        deque.add("a3");
        deque.push("a4");
        deque.pop();
        String tmp = deque.pop();
        Assert.assertEquals("1", tmp);
    }

    @Test
    public void testArrayDequeOffer() {
        ArrayDeque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("3");
        deque.add("4");
        deque.offer("5");
        String tmp = deque.pollLast();
        Assert.assertEquals("5", tmp);
    }

    @Test
    public void testArrayDequeOfferFirst() {
        ArrayDeque deque = new ArrayDeque<>();
        deque.add("1");
        deque.add("2");
        deque.add("3");
        deque.add("4");
        deque.offerFirst("5");
        String tmp = deque.peekFirst();
        Assert.assertEquals("5", tmp);
    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5677363.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存