💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# `LinkedList push()`和`pop()`方法 > 原文: [https://beginnersbook.com/2014/08/linkedlist-push-and-pop-methods-java/](https://beginnersbook.com/2014/08/linkedlist-push-and-pop-methods-java/) #### 描述 用于演示`LinkedList`上的推送和弹出操作的程序。 #### `LinkedList.push(E e)` `public void push(E e)`:将元素插入列表的前面。 **示例:** ```java 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); } } ``` **输出:** ```java LinkedList contains: [Jack, Robert, Chaitanya, kate] LinkedList contains: [NEW ELEMENT, Jack, Robert, Chaitanya, kate] ``` #### `LinkedList.pop()` `public E pop()`:删除并返回列表的第一个元素。 **示例:** ```java 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); } } ``` **输出:** ```java LinkedList before: [Jack, Robert, Chaitanya, kate] Element removed: Jack LinkedList after: [Robert, Chaitanya, kate] ```