Java 集合 LinkedList push()和pop()方法

用于演示LinkedList上的推送和弹出操作的程序。

LinkedList.push(E e)

public void push(E e):将元素插入列表的前面。

示例:

import java.util.LinkedList;
class LinkedListExample {

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Jack");
     list.add("Robert");
     list.add("Chaitanya");
     list.add("kate");

     // Display LinkList elements
     System.out.println("LinkedList contains: "+list);

     // push Element the list
     list.push("NEW ELEMENT");

     // Display after push operation
     System.out.println("LinkedList contains: "+list);
  }
}

输出:

LinkedList contains: [Jack, Robert, Chaitanya, kate]
LinkedList contains: [NEW ELEMENT, Jack, Robert, Chaitanya, kate]

LinkedList.pop()

public E pop():删除并返回列表的第一个元素。

示例:

import java.util.LinkedList;
class LinkedListPopDemo{

  public static void main(String[] args) {

     // Create a LinkedList of Strings
     LinkedList<String> list = new LinkedList<String>();

     // Add few Elements
     list.add("Jack");
     list.add("Robert");
     list.add("Chaitanya");
     list.add("kate");

     // Display LinkList elements
     System.out.println("LinkedList before: "+list);

     // pop Element from list and display it
     System.out.println("Element removed: "+list.pop());

     // Display after pop operation
     System.out.println("LinkedList after: "+list);
  }
}

输出:

LinkedList before: [Jack, Robert, Chaitanya, kate]
Element removed: Jack
LinkedList after: [Robert, Chaitanya, kate]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程