LinkedList 是 Java Collection Framework 的通用类,它实现了 List、Deque 和 Queue 三个接口。它提供了 LinkedList 数据结构的功能,LinkedList 是一种线性数据结构,其中每个元素相互链接。我们可以对 LinkedList 执行多种操作,包括添加、删除和遍历元素。要将元素添加到 LinkedList 集合中,我们可以使用各种内置方法,例如 add()、addFirst() 和 addLast()。我们将探索如何使用这些方法将元素添加到 LinkedList。
在 Java 中向 LinkedList 添加元素
在Java中,LinkedList类提供以下内置方法来添加元素 -
add() − 它用于将元素插入到集合的末尾。我们更频繁地使用它比其他方法。
addFirst() − 此方法用于在LinkedList的第一个索引处插入一个元素。
addLast() − 它将一个元素插入到指定LinkedList的最后一个索引位置。
addAll() − 当我们需要将一个集合的所有元素添加到另一个LinkedList中时,我们使用addAll()方法。
在我们的 Java 程序中,在将元素插入 LinkedList 时,我们只需将所需的元素作为参数传递即可。
在跳转到Java程序以向LinkedList添加元素之前,让我们讨论一下创建LinkedList类实例的语法。
语法
LinkedList<Type> nameOfinstance = new LinkedList<>();
在这里,'Type'可以是任何包装类。
Example 1
的中文翻译为:示例1
下面的示例说明了我们如何使用add()方法向LinkedList中插入元素。
import java.util.LinkedList; public class Demo1 { public static void main(String[] args) { // creating a linkedlist LinkedList<String> input_list = new LinkedList<>(); // adding elements to the list input_list.add("Java"); input_list.add("Python"); input_list.add("Scala"); input_list.add("Shell"); // printing the result System.out.println("The elements added to the list are: " + input_list); } }
输出
The elements added to the list are: [Java, Python, Scala, Shell]
示例 2
也可以使用add()方法在LinkedList的所需位置插入元素。它还可以接受索引号作为可选参数。
import java.util.LinkedList; public class Demo2 { public static void main(String[] args) { // creating a linkedlist LinkedList<String> input_list = new LinkedList<>(); // adding initial elements to the list input_list.add("Java"); input_list.add("Python"); input_list.add("JavaScript"); // printing the result System.out.println("The list is defined as: " + input_list); // adding a new element to the existing list at index 1 input_list.add(1, "Scala"); // printing the new result System.out.println("The list after adding element at position 1: "); int index = 0; for(String print : input_list) { System.out.println("Index: " + index + ", Element: " + print); index++; } } }
输出
The list is defined as: [Java, Python, JavaScript] The list after adding element at position 1: Index: 0, Element: Java Index: 1, Element: Scala Index: 2, Element: Python Index: 3, Element: JavaScript
示例 3
在下面的示例中,我们将使用 listIterator() 方法向 LinkedList 添加元素。
import java.util.LinkedList; import java.util.ListIterator; public class Demo3 { public static void main(String[] args) { // creating a linkedlist LinkedList<String> input_list = new LinkedList<>(); // creating an instance of ListIterator ListIterator<String> newList = input_list.listIterator(); // adding elements to the list newList.add("Java"); newList.add("Python"); newList.add("Scala"); newList.add("Shell"); // printing the result System.out.println("The elements added to the list are: " + input_list); } }
输出
The elements added to the list are: [Java, Python, Scala, Shell]
Example 4
的中文翻译为:示例4
在这个例子中,我们将使用addFirst()和addLast()方法在LinkedList的第一个和最后一个索引处插入元素。
import java.util.LinkedList; public class Demo4 { public static void main(String[] args) { LinkedList<Integer> inputList = new LinkedList<>(); // Adding elements in linkedlist inputList.add(8); inputList.add(4); inputList.add(1); inputList.add(0); System.out.println("Elements of the original Linkedlist : " + inputList); // adding elements to the first and last index inputList.addFirst(9); inputList.addLast(9); // to print the result System.out.println("After adding elements to the first and last index of Linkedlist : " + inputList); } }
输出
Elements of the original Linkedlist : [8, 4, 1, 0] After adding elements to the first and last index of Linkedlist : [9, 8, 4, 1, 0, 9]
结论
本文首先介绍了 LinkedList,它是 Java Collection Framework 的通用类。在下一节中,我们看到了此类的各种内置方法,可用于将元素插入 LinkedList 集合。这些方法是:add()、addAll()、addFirst() 和 addLast()。
以上是Java程序向LinkedList添加元素的详细内容。更多信息请关注PHP中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

WebStorm Mac版
好用的JavaScript开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版
视觉化网页开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。