首頁 > Java > java教程 > 主體

爪哇流行音樂

王林
發布: 2024-08-30 15:57:28
原創
580 人瀏覽過

在Java中,pop是一種移除堆疊、陣列、LinkedList等中元素的方法。可以使用Stack.pop()方法從堆疊中移除元素,並且會從堆疊頂部移除。對於 LinkedList,LinkedListObject.pop() 方法用於從 LinkedList 表示的堆疊頂部刪除。 ArrayDequere 中的 LinkedBlockingDeque.pop() 方法將元素從 Deque 的頂部移動。有關它們的更多詳細資訊將在以下部分中討論。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

文法

讓我們來看看堆疊、LinkedList 和 Deque 中 pop 方法的語法。

1.堆疊

文法:

STACK.pop()
登入後複製

參數:此方法沒有參數。

傳回值: 傳回元素並將其從堆疊頂部刪除。

2. LinkedListObject

文法:

LinkedListObject.pop()
登入後複製

參數:此方法沒有參數。

傳回類型: 返回元素並將其從堆疊頂部刪除。

3. LinkedBlockingDeque

文法:

LinkedBlockingDeque.pop()
登入後複製

參數:此方法沒有參數。

傳回值: 傳回元素並將其從堆疊頂部刪除。

Java 中的 pop 方法是如何運作的?

pop 方法在 Stack、LinkedList 和 Deque 中的工作原理類似。為此,可以執行以下步驟。

1.根據需求建立堆疊、鍊錶或雙端佇列。

Stack<String>s = new Stack<String>( );
LinkedList<Integer>li = newLinkedList<>( );
LinkedBlockingDeque<String> li = new LinkedBlockingDeque<String>(capacity);
登入後複製

2.使用 pop 方法從中刪除元素。

s.pop()
li.pop()
dq.pop()
登入後複製

Java Pop 範例

下面提到了不同的例子:

範例#1

從堆疊中彈出字串類型元素的Java程式。

代碼:

import java.util.*;
public class PopMethodExample
{
public static void main(String args[])
{
// create a stack
Stack<String> st = new Stack<String>();
st.push("Happy");
st.push("Sad");
st.push("Confused");
st.push("Tensed");
st.push("Mixed Emotions");
// Print elements in stack
System.out.println("Stack Elements: " + st );
// Pop elements
st.pop();
st.pop();
// Stack after removing new elements
System.out.println("Stack after removing elements " + st);
}
}
登入後複製

輸出:

爪哇流行音樂

首先,建立一個堆疊並使用push()方法新增元素。然後,列印堆疊並使用 pop() 方法刪除元素。由於呼叫了兩個 pop() 方法,因此在執行程式碼時將刪除堆疊頂部的兩個元素。最後,列印刪除元素後的堆疊。

範例#2

從 LinkedList 中彈出整數型元素的 Java 程式。

代碼:

import java.util.*;
public class PopMethodExample
{
public static void main(String args[])
{
// create a LinkedList
LinkedList<Integer> lis = new LinkedList<>();
lis.push(45);
lis.push(90);
lis.push(67);
lis.push(33);
lis.push(56);
// Print elements in LinkedList
System.out.println("LinkedList Elements: " + lis);
// Pop elements
lis.pop();
lis.pop();
// LinkedList after removing elements
System.out.println("LinkedList after removing elements " + lis);
}
}
登入後複製

輸出:

爪哇流行音樂

建立一個LinkedList並使用push()方法加入整數類型的元素。然後,列印 LinkedList 並使用 pop() 方法刪除元素。執行程式碼時,可以顯示刪除元素之前和刪除元素之後的LinkedList。

範例#3

從堆疊中刪除整數元素的 Java 程式。

代碼:

import java.util.*;
public class PopMethodExample
{
public static void main(String args[])
{
// create a stack
Stack<Integer> st = new Stack<Integer>();
st.push(45);
st.push(90);
st.push(67);
st.push(33);
st.push(56);
// Print elements in stack
System.out.println("stack Elements: " + st);
// Pop elements
st.pop();
st.pop();
// stack after removing elements
System.out.println("stack after removing elements " + st);
}
}
登入後複製

輸出:

爪哇流行音樂

先建立一個接受整數元素的堆疊。建立後,將使用 push() 方法將元素新增至堆疊中。列印堆疊中的目前元素後,將從其中刪除兩個元素。為了檢查這些元素是否來自堆疊,元素會被列印一次。  執行程式碼時,可以看到從堆疊中刪除了兩個元素。

範例#4

從 LinkedList 中刪除字串元素的 Java 程式。

代碼:

import java.util.*;
public class PopMethodExample
{
public static void main(String args[])
{
// create a LinkedList
LinkedList<String> lis = new LinkedList<>();
lis.push("Happy");
lis.push("Sad");
lis.push("Confused");
lis.push("Tensed");
lis.push("Mixed Emotions");
// Print elements in LinkedList
System.out.println("LinkedList Elements: " + lis );
// Pop elements
lis.pop() ;
lis.pop() ;
// LinkedList after removing elements
System.out.println("LinkedList after removing elements " + lis ) ;
}
}
登入後複製

輸出:

爪哇流行音樂

建立一個LinkedList li,並使用push()方法加入字串型別的元素。然後,列印 LinkedList 並使用 pop() 方法刪除元素。執行程式碼時,可以顯示刪除元素之前和刪除元素之後的LinkedList。

範例#5

從 LinkedBlockigDeque 中刪除字串元素的 Java 程式。

代碼:

import java.util.concurrent.LinkedBlockingDeque;
public class PopMethodExample {
public static void main(String[] args) {
LinkedBlockingDeque<String> dq = new LinkedBlockingDeque<String>(100);
dq.add("Hia");
dq.add("Ahi");
dq.add("Liba");
dq.add("Geru");
//Removes element from the top of the stack
String s = dq.pop();
System.out.println("First stack element  : "+ s);
System.out.println("Stack after removing element : "+ dq);
}
}
登入後複製

輸出:

爪哇流行音樂

Create a deque for adding the elements. For that, use the method add() and add the elements of string type. Then, print the Deque and identify the current elements present in it. After printing the current elements in the stack, remove the first element from the stack. For checking whether those elements are removed from the Deque, elements are printed one more time.  On executing the code, it can be seen that one element is removed from the Deque.

Example #6

Java program to remove integer elements from LinkedBlockigDeque.

Code:

import java.util.concurrent.LinkedBlockingDeque;
public class PopMethodExample {
public static void main(String[] args) {
LinkedBlockingDeque<Integer> dq = new LinkedBlockingDeque<Integer>(100);
dq.add(34);
dq.add(45);
dq.add(56);
//Removes element from the top of the stack
Integer i = dq.pop();
System.out.println("First stack element  : "+ i );
System.out.println("Stack after removing element : "+ dq);
}
}
登入後複製

Output:

爪哇流行音樂

Unlike the above program, elements of integer type are added using add() method. On executing the code, it can be seen that one element is removed from the Deque.

Conclusion

Pop is a method that is used to remove elements from the stack, LinkedList, array with the help of Stack.pop(), LinkedList.pop() and LinkedBlockingDeque.pop()  respectively. In this article, details such as syntax, working, and example of the pop method is explained in detail.

以上是爪哇流行音樂的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!