您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Java程序向LinkedList添加元素

2025/5/24 8:45:16发布25次查看
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: javaindex: 1, element: scalaindex: 2, element: pythonindex: 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添加元素的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product