📜  Java Java类设置 2

📅  最后修改于: 2022-05-13 01:54:53.968000             🧑  作者: Mango

Java Java类设置 2

Java Java类设置 1
util.ArrayDeque 类的更多方法:

16. offer(Element e) : Java.util.ArrayDeque.offer(Element e) : 在双端队列的末尾插入元素。
句法 :

public boolean offer(Element e)
Parameters : 
e - element to add
Return : 
true, if element is added else; false

17. offerFirst(Element e) : Java.util.ArrayDeque.offerFirst(Element e) : 在双端队列的前面插入元素。
句法 :

public boolean offerFirst(Element e)
Parameters : 
e - element to add
Return : 
true, if element is added else; false

18. offerLast(Element e) : Java.util.ArrayDeque.offerLast(Element e) : 在双端队列末尾插入元素。
句法 :

public boolean offerLast(Element e)
Parameters : 
e - element to add
Return : 
true, if element is added else; false

19. peek() : Java.util.ArrayDeque.peek() : 返回头元素而不删除它。
句法 :

public E peek()
Parameters :
--------
Return : 
head of deque or null if deque is empty.

20. peekFirst() : Java.util.ArrayDeque.peekFirst() : 返回第一个元素而不删除它。
句法 :

public E peekFirst()
Parameters :
--------
Return : 
first element of deque or null if deque is empty.

21. peekLast() : Java.util.ArrayDeque.peek() : 返回最后一个元素而不删除它。
句法 :

public E peekLast()
Parameters :
--------
Return : 
Last element of deque or null if deque is empty.

22. poll() : Java.util.ArrayDeque.peek() : 返回头元素并删除它
句法 :

public E poll()
Parameters :
--------
Return : 
head of deque or null if deque is empty.

23. pollFirst() : Java.util.ArrayDeque.peek() : 返回第一个元素并删除它
句法 :

public E pollFirst()
Parameters :
--------
Return : 
first element of deque or null if deque is empty.

24. pollLast() : Java.util.ArrayDeque.peek() : 返回最后一个元素并删除它
句法 :

public E pollLast()
Parameters :
--------
Return : 
last element of deque or null if deque is empty.

25. pop() : Java.util.ArrayDeque.pop() : 弹出一个由 deque 重复的堆栈元素
句法 :

public E pop()
Parameters :
---------
Return : 
element at front

26. push(Element e) : Java.util.ArrayDeque.push(Element e) : 将元素推送到由双端队列表示的堆栈上
句法 :

public void push(Element e)
Parameters :
e : element to be pushed
Return : 
----------

27. remove() : Java.util.ArrayDeque.remove() : 返回头元素并删除它
句法 :

public E remove()
Parameters :
------
Return : 
head of the deque

28. removeFirst() : Java.util.ArrayDeque.remove() : 返回第一个元素并删除它
句法 :

public E removeFirst()
Parameters :
------
Return : 
first element of the deque

29. removeLast() : Java.util.ArrayDeque.remove() : 返回最后一个元素并删除它
句法 :

public E removeLast()
Parameters :
------
Return : 
last element of the deque

30. removeFirstOccurrence(Obj) : Java.util.ArrayDeque.removeFirstOccurrence(Obj) : 删除它在双端队列中第一次出现的元素。
句法 :

public boolean removeFirstOccurrence(Object obj)
Parameters :
obj : element to be removed
Return : 
true, if the element is removed; else False

31. removeLastOccurrence(Obj) : Java.util.ArrayDeque.removeLastOccurrence(Obj) : 删除它在双端队列中最后出现的元素。
句法 :

public boolean removeLastOccurrence(Object obj)
Parameters :
obj : element to be removed
Return : 
true, if the element is removed; else False

解释 util.ArrayDeque 类方法的Java程序:

Java
// Java code explaining the use of ArrayDeque class methods
// offer(), offerFirst(), offerLast(), peek(), peekFirst(), peekLast()
// poll(), peekFirst(), peekLast(), pop(), push()
 
import java.util.*;
public class NewClass
{
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque d = new ArrayDeque(10);
 
        // add() method to insert
        d.add(2);
        d.add(4);
 
        // offer() : add element at end
        d.offer(100);
        d.offer(101);
 
        // offerFirst() : add element at start
        d.offerFirst(1111);
        d.offerFirst(3333);
 
        // offerLast() : add element at end
        d.offerLast(5000);
        d.offerLast(50001);
 
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
 
        // peek() method : to get head
        System.out.println("\nHead element : " + d.peek());
 
        // peekFirst() method : to get First element
        System.out.println("First element : " + d.peekFirst());
 
        // peekLast() method : to get Last element
        System.out.println("Last element : " + d.peekLast());
 
 
        // poll() method : to get head
        System.out.println("\nHead element poll : " + d.poll());
 
        // pollFirst() method : to get First element
        System.out.println("First element poll : " + d.pollFirst());
 
        // pollLast() method : to get Last element
        System.out.println("Last element poll : " + d.pollLast() + "\n");
 
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
 
        // pop() method :
        System.out.println("Pop element : " + d.pop());
 
        // push() method :
        d.push(11010101);
        d.push(121212121);
        d.push(131313131);
 
        // remove() method : to get head
        System.out.println("\nHead element remove : " + d.remove());
 
        // removeFirst() method : to get First element
        System.out.println("First element remove : " + d.removeFirst());
 
        // removeLast() method : to get Last element
        System.out.println("Last element remove : " + d.removeLast() + "\n");
 
        for (Integer element : d)
        {
            System.out.println("Element : " + element);
        }
 
    }
}


输出 :

Element : 3333
Element : 1111
Element : 2
Element : 4
Element : 100
Element : 101
Element : 5000
Element : 50001

Head element : 3333
First element : 3333
Last element : 50001

Head element poll : 3333
First element poll : 1111
Last element poll : 50001

Element : 2
Element : 4
Element : 100
Element : 101
Element : 5000
Pop element : 2

Head element remove : 131313131
First element remove : 121212121
Last element remove : 5000

Element : 11010101
Element : 4
Element : 100
Element : 101