Java中的LinkedList是與陣列不同的線性資料結構。在 Java 程式中使用鍊錶有一定的優點和缺點。鍊錶中的每個元素都儲存在稱為節點的單元中。每個節點都有一個特定的位址。 LinkedList 的主要缺點是每個點的節點不容易存取。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
每個節點都有指標來定址,要存取特定的節點,必須從頭開始,然後到達要存取的節點的特定指標。與其他 Java 介面一樣,LinkedList 類別也包含許多建構子和方法。在本文中,我們將看到 LinkedList 中使用的兩個建構子。
他們是:
Java LinkedList 類別中有許多方法或函數。我們將在本文中看到 Java LinkedList 類別中的一些函數。
他們是:
下面給出了提到的範例:
在這個編碼範例中,我們將看到 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); } }
輸出:
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.
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()); } } }
Output:
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.
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()); } } }
Output:
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.
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中文網其他相關文章!