搜尋
首頁Javajava教程Java 中的鍊錶

Java 中的鍊錶

Aug 30, 2024 pm 03:48 PM
java

Java中的LinkedList是與陣列不同的線性資料結構。在 Java 程式中使用鍊錶有一定的優點和缺點。鍊錶中的每個元素都儲存在稱為節點的單元中。每個節點都有一個特定的位址。 LinkedList 的主要缺點是每個點的節點不容易存取。

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

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

每個節點都有指標來定址,要存取特定的節點,必須從頭開始,然後到達要存取的節點的特定指標。與其他 Java 介面一樣,LinkedList 類別也包含許多建構子和方法。在本文中,我們將看到 LinkedList 中使用的兩個建構子。

他們是:

  • LinkedList(): 用來分配一個空的 LinkedList()。
  • LinkedList(Collection C):這用於建立一個包含指定集合的​​所有元素的有序列表,由集合的迭代器傳回。

Java中LinkedList的方法

Java LinkedList 類別中有許多方法或函數。我們將在本文中看到 Java LinkedList 類別中的一些函數。

他們是:

  • add(int a, I Element):此方法用於在此清單中的特定位置插入特定元素。
  • add( E e): 此方法將指定元素固定在清單末端。
  • add(int index, Collection C): 此方法從起始位置開始插入清單中所有指定的元素。
  • offerFirst(): 此方法將指定元素插入此清單的前面。
  • addLast():此方法用於在清單末尾插入元素。
  • voidclear():此方法用於刪除Linkedlist中的所有元素。
  • poll(): 刪除清單的第一個元素。
  • lastIndexOf(): 用於傳回指定元素在此清單中最後一次出現的索引。
  • getLast(): 函數用於傳回 LinkedList 中的最後一個元素。
  • offer(): 此方法插入指定元素作為清單的尾部元素。
  • offerLast(): 此方法在此清單末尾插入指定元素。
  • peek(): 它檢索清單的第一個元素。
  • peekFirst(): 此方法用於檢索清單的最後一個元素,如果清單為空,則傳回 null。
  • addFirst(): 此方法用於將元素插入到清單的開頭。
  • peekLast(): 此方法用於檢索清單的最後一個元素,如果清單為空,則傳回 null。
  • pollFirst(): 此方法用於檢索和刪除此清單的第一個元素,如果此清單為空,則傳回 null。
  • contains():如果 LinkedList 包含節點處的特定元素,則此函數傳回 true。
  • pollLast(): 此方法刪除此清單的最後一個元素,如果此清單為空,則傳回 null。
  • removeFirst(): 此方法傳回此清單中的第一個元素。
  • element():此方法檢索但不刪除清單的頭部。
  • getFirst():方法用來傳回LinkedList的第一個元素。
  • remove(): 此方法刪除 LinkedList 的第一個元素。
  • remove(int index): 此方法刪除此清單中指定位置的元素。
  • removeLast(): 此方法傳回此清單中的最後一個元素。
  • set(int index, E element): 此方法將此清單中指定位置的元素替換為指定元素。
  • size(): 此方法傳回此清單中的元素數量。

Java 中 LinkedList 的範例

下面給出了提到的範例:

範例#1

在這個編碼範例中,我們將看到 LinkedList 方法,在鍊錶中插入某些元素,然後刪除它們,最後顯示鍊錶。

代碼:

import java.util.*;
public class Example3
{
public static void main(String args[])
{
LinkedList<string> object = new LinkedList<string>();
// Adding elements to the linked list
object.add("A");
object.add("B");
object.addLast("C");
object.addFirst("D");
object.add(2, "E");
object.add("F");
object.add("G");
System.out.println("Linked list : " + object);
object.remove("C");
object.remove(3);
object.removeFirst();
object.removeLast();
System.out.println("Linked list after deletion: " + object);
}
}</string></string>

輸出:

Java 中的鍊錶

In the sample output, we see that there are certain elements in the linkedlist, and finally, certain elements are deleted, and then the linkedlist after all the deletion of the elements is shown.

Example #2

In this program, we are going to see four names being printed using sequential order in LinkedList. We use a String LinkedList and use it to print names that can be of any number. We use the While loop here for printing the names which are present in the program.

Code:

import java.util.*;
public class LinkedList1
{
public static void main(String args[])
{
LinkedList<string> al=new LinkedList<string>();
al.add("Ishankamal Mitra");
al.add("Sourya Mukherjee");
al.add("Satyaki Das");
al.add("Debodooty Sarkar");
Iterator<string> itr=al.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}</string></string></string>

Output:

Java 中的鍊錶

In this program, we check how the coding helps us to print four names in sequential order as mentioned in the LinkedList. In the next program, we are going to see how the sequence is changed; that is, the names are printed in reverse order of the input.

Example #3

In this code, the program inputs the name and then prints the names in the reverse order of their sequence.

Code:

import java.util.*;
public class LinkedList4
{
public static void main(String args[])
{
LinkedList<string> ll=new LinkedList<string>();
ll.add("Ishankamal Mitra");
ll.add("Sourya Mukherjee");
ll.add("Satyaki Das");
//Going through the list of elements in Reverse order
Iterator i=ll.descendingIterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}</string></string>

Output:

Java 中的鍊錶

In this program, we use the DescendingIterator(), and we use it to print the names in the reverse order of the input. We can see it very clearly through the program.

Conclusion

In this article, we saw the different constructors and methods which are present in the LinkedList class. Plus, we saw a Java program to illustrate the insertion and deletion of elements in a LinkedList. We also saw the advantages and disadvantages of using LinkedList over arrays. They contain nodes that are not easily accessible and have to be accessed through the LinkedList head. We also notice three examples of coding where names are printed in reverse order, sequential order, and removing elements from a LinkedList. These programs help us to understand the methodology of the LinkedList class.

以上是Java 中的鍊錶的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
Java開發的哪些方面取決於平台?Java開發的哪些方面取決於平台?Apr 26, 2025 am 12:19 AM

JavadevelovermentIrelyPlatForm-DeTueTososeVeralFactors.1)JVMVariationsAffectPerformanceNandBehaviorAcroSsdifferentos.2)Nativelibrariesviajnijniiniininiinniinindrododerplatefform.3)

在不同平台上運行Java代碼時是否存在性能差異?為什麼?在不同平台上運行Java代碼時是否存在性能差異?為什麼?Apr 26, 2025 am 12:15 AM

Java代碼在不同平台上運行時會有性能差異。 1)JVM的實現和優化策略不同,如OracleJDK和OpenJDK。 2)操作系統的特性,如內存管理和線程調度,也會影響性能。 3)可以通過選擇合適的JVM、調整JVM參數和代碼優化來提升性能。

Java平台獨立性有什麼局限性?Java平台獨立性有什麼局限性?Apr 26, 2025 am 12:10 AM

Java'splatFormentenceHaslimitations不包括PerformanceOverhead,versionCompatibilityIsissues,挑戰WithnativelibraryIntegration,Platform-SpecificFeatures,andjvminstallation/jvminstallation/jvmintenance/jeartenance.therefactorscomplicatorscomplicatethe“ writeOnce”

解釋平台獨立性和跨平台發展之間的差異。解釋平台獨立性和跨平台發展之間的差異。Apr 26, 2025 am 12:08 AM

PlatformIndependendecealLowsProgramStormonanyPlograwsStormanyPlatFormWithOutModification,而LileCross-PlatFormDevelopmentRequiredquiresMomePlatform-specificAdjustments.platFormIndependence,EneblesuniveByjava,EnablesuniversUniversAleversalexecutionbutmayCotutionButMayComproMisePerformance.cross.cross.cross-platformd

即時(JIT)彙編如何影響Java的性能和平台獨立性?即時(JIT)彙編如何影響Java的性能和平台獨立性?Apr 26, 2025 am 12:02 AM

JITcompilationinJavaenhancesperformancewhilemaintainingplatformindependence.1)Itdynamicallytranslatesbytecodeintonativemachinecodeatruntime,optimizingfrequentlyusedcode.2)TheJVMremainsplatform-independent,allowingthesameJavaapplicationtorunondifferen

為什麼Java是開發跨平台桌面應用程序的流行選擇?為什麼Java是開發跨平台桌面應用程序的流行選擇?Apr 25, 2025 am 12:23 AM

javaispopularforcross-platformdesktopapplicationsduetoits“ writeonce,runany where”哲學。 1)itusesbytiesebyTecodeThatrunsonAnyJvm-備用Platform.2)librarieslikeslikeslikeswingingandjavafxhelpcreatenative-lookingenative-lookinguisis.3)

討論可能需要在Java中編寫平台特定代碼的情況。討論可能需要在Java中編寫平台特定代碼的情況。Apr 25, 2025 am 12:22 AM

在Java中編寫平台特定代碼的原因包括訪問特定操作系統功能、與特定硬件交互和優化性能。 1)使用JNA或JNI訪問Windows註冊表;2)通過JNI與Linux特定硬件驅動程序交互;3)通過JNI使用Metal優化macOS上的遊戲性能。儘管如此,編寫平台特定代碼會影響代碼的可移植性、增加複雜性、可能帶來性能開銷和安全風險。

與平台獨立性相關的Java開發的未來趨勢是什麼?與平台獨立性相關的Java開發的未來趨勢是什麼?Apr 25, 2025 am 12:12 AM

Java將通過雲原生應用、多平台部署和跨語言互操作進一步提昇平台獨立性。 1)雲原生應用將使用GraalVM和Quarkus提升啟動速度。 2)Java將擴展到嵌入式設備、移動設備和量子計算機。 3)通過GraalVM,Java將與Python、JavaScript等語言無縫集成,增強跨語言互操作性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用