Use of set (index, object) and add (index, object)/Stack empty() method and isEmpty() method/deque deque in ArrayList and some principles

Source:
Disclaimer: If I violate anyone's rights, please contact me and I will delete it.
Welcome experts to spray me

ArrayList中set(index,object)与add(index,object)

set: replace the object at
the original index position add: move the original index position backward

List list=new ArrayList();
list.add(index,obj);
put the index+1 and index+ After one element is moved back one bit, similar to elementData[i+1]=elementData[i];
and then put obj in the index position.
Of course, this process needs to consider the rationality of the index and whether it will overflow.

Stack's empty() method and isEmpty() method

If there are no elements in the array, it returns true.
If there are elements in the array, it returns false.

Stack inherits the method in class java.util.Vector
Here is the method in vector:
Insert picture description here
here is the method in
Stack: The empty() function in Stack will call the size() function in vector and then judge whether it is empty.
Insert picture description here
Summary: There should be no difference

Deque use and some principles

Reference: https://www.cnblogs.com/denglh/p/7911513.html

import java.util.Deque;
import java.util.LinkedList;

public class DequeTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        
        Deque<String> deque = new LinkedList<String>();
        deque.add("d");
        deque.add("e");
        deque.add("f");
        
        //从队首取出元素,不会删除
        System.out.println("队首取出元素:"+deque.peek());
        System.out.println("队列为:"+deque);
        
        //从队首加入元素(队列有容量限制时用,无则用addFirst)
        deque.offerFirst("c");
        System.out.println("队首加入元素后为:"+deque);
        //从队尾加入元素(队列有容量限制时用,无则用addLast)
        deque.offerLast("g");
        System.out.println("队尾加入元素后为:"+deque);
        
        //队尾加入元素
        deque.offer("h");
        System.out.println("队尾加入元素后为:"+deque);
        
        //获取并移除队列第一个元素,pollFirst()也是,区别在于队列为空时,removeFirst会抛出NoSuchElementException异常,后者返回null
        deque.removeFirst();
        System.out.println("获取并移除队列第一个元素后为:"+deque);
        
        //获取并移除队列第一个元素,此方法与pollLast 唯一区别在于队列为空时,removeLast会抛出NoSuchElementException异常,后者返回null
        deque.removeLast();
        System.out.println("获取并移除队列最后一个元素后为:"+deque);
        
        //获取队列第一个元素.此方法与 peekFirst 唯一的不同在于:如果此双端队列为空,它将抛出NoSuchElementException,后者返回null
        System.out.println("获取队列第一个元素为:"+deque.getFirst());
        System.out.println("获取队列第一个元素后为:"+deque);
        
        //获取队列最后一个元素.此方法与 peekLast 唯一的不同在于:如果此双端队列为空,它将抛出NoSuchElementException,后者返回null
        System.out.println("获取队列最后一个元素为:"+deque.getLast());
        System.out.println("获取队列第一个元素后为:"+deque);
        
        //循环获取元素并在队列移除元素
        while(deque.size()>0){
            System.out.println("获取元素为:"+ deque.pop()+" 并删除");
        }
        System.out.println("队列为:"+deque);
    }

}

Output

队首取出元素:d
队列为:[d, e, f]
队首加入元素后为:[c, d, e, f]
队尾加入元素后为:[c, d, e, f, g]
队尾加入元素后为:[c, d, e, f, g, h]
获取并移除队列第一个元素后为:[d, e, f, g, h]
获取并移除队列最后一个元素后为:[d, e, f, g]
获取队列第一个元素为:d
获取队列第一个元素后为:[d, e, f, g]
获取队列最后一个元素为:g
获取队列第一个元素后为:[d, e, f, g]
获取元素为:d 并删除
获取元素为:e 并删除
获取元素为:f 并删除
获取元素为:g 并删除
队列为:[]

The inheritance relationship is: deque => queue => collection=" Iterable
1. When using queues, why use deque to receive when new LinkedList is used instead of LinkedList?
  Answer: deque inherits the queue interface because it has two implementations, LinkedList and ArrayDeque. Receiving with deque is because of the up-casting (the subclass transfers to the parent class, and the special functions of the subclass will be lost). You can try it. Use the get() method to receive LinkedList.
2. Why is it not enough to have one implementation, but two? There are always differences between them?
  Answer: ArrayDeque is a Deque implemented based on head and tail pointers, which means that the first and last elements cannot be accessed. If you want to use iterators, you can iterate forward and backward.
    ArrayDeque is generally better than linked list queue/double-ended queue, and a limited amount of garbage is generated (old arrays will be discarded in the expansion), it is recommended to use deque, ArrayDeque is preferred.

Guess you like

Origin blog.csdn.net/qq_45531729/article/details/111381796