LAPTOP-8KA88UT5_20200908-陈浩

问题

问题:根据 deque.toString() 对应的字符串为参照来理解头和尾这一部分

解决:Deque默认栈操作,通过字符串形式确定栈顶

Deque<String> deque = new LinkedList<>();
        
        /**===以字符串形式右侧为栈顶===========================*/
        deque.offerLast( "1" );
        deque.offerLast( "2" );
        deque.offerLast( "3" );
        deque.offerLast( "4" );
        
        System.out.println( deque );//输出[1,2,3,4]
        System.out.println( deque.peekLast() );//输出4
        System.out.println( deque );//输出[1,2,3,4]
        System.out.println( deque.pollLast() );//输出4
        System.out.println( deque );//输出[1,2,3]
        
        deque.clear();
        /**===以字符串形式左侧为栈顶===========================*/
        deque.offerFirst( "A" );
        deque.offerFirst( "B" );
        deque.offerFirst( "C" );
        System.out.println( deque );//输出[C,B,A]
        
        System.out.println( deque.peekFirst() );//输出C
        System.out.println( deque );//输出[C,B,A]
        
        System.out.println( deque.pollFirst() );//输出C
        System.out.println( deque );//输出[B,A]       

吐槽

标签

© 2021 成都云创动力科技有限公司 蜀ICP备20006351号-1