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中文网其他相关文章!