LinkedList pollLast() trong java với ví dụ cụ thể

LinkedList pollLast() trả về phần tử cuối cùng và xoá nó ra khởi LinkedList.

Syntax

public E pollLast();

Ví dụ

import java.util.LinkedList;

public class Main {
    public static void main(String args[]) {
        LinkedList<String> linkedList = new LinkedList<>();
        linkedList.add("HCMUS");
        linkedList.add("UIT");
        linkedList.add("HCMUT");

        System.out.println("LinkedList ban dau: " + linkedList);
        System.out.println("Phan tu cuoi cung: " + linkedList.pollLast());
        System.out.println("LinkedList sau pollLast: " + linkedList);

    }
}

Output:

LinkedList ban dau: [HCMUS, UIT, HCMUT]
Phan tu cuoi cung: HCMUT
LinkedList sau pollLast: [HCMUS, UIT]

Note: Nếu LinkedList rỗng thì pollLast() sẽ trả về null

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x