目录
Java中LinkedList的方法
Java 中 LinkedList 的示例
示例#1
Example #3
Conclusion
首页 Java java教程 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);
}
}
登录后复制

输出:

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

Java 中的平方根 Java 中的平方根 Aug 30, 2024 pm 04:26 PM

Java 中的平方根

Java 中的完美数 Java 中的完美数 Aug 30, 2024 pm 04:28 PM

Java 中的完美数

Java 中的随机数生成器 Java 中的随机数生成器 Aug 30, 2024 pm 04:27 PM

Java 中的随机数生成器

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java中的Weka

Java 中的阿姆斯特朗数 Java 中的阿姆斯特朗数 Aug 30, 2024 pm 04:26 PM

Java 中的阿姆斯特朗数

Java 中的史密斯数 Java 中的史密斯数 Aug 30, 2024 pm 04:28 PM

Java 中的史密斯数

Java Spring 面试题 Java Spring 面试题 Aug 30, 2024 pm 04:29 PM

Java Spring 面试题

突破或从Java 8流返回? 突破或从Java 8流返回? Feb 07, 2025 pm 12:09 PM

突破或从Java 8流返回?

See all articles