首页 > 文章列表 > Java程序向LinkedList添加元素

Java程序向LinkedList添加元素

java linkedlist 添加元素
369 2023-08-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: 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()。