首頁  >  文章  >  Java  >  Java 中的鍊錶

Java 中的鍊錶

PHPz
PHPz原創
2024-08-30 15:48:16529瀏覽

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);
}
}

輸出:

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());
}
}
}

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());
}
}
}

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