Java 向LinkedList中添加元素

Java 向LinkedList中添加元素

LinkedList是Java集合框架的一个泛型类,实现了List、Deque和Queue三个接口。它提供了LinkedList数据结构的特性,即每个元素都与其他元素链接在一起的线性数据结构。我们可以对LinkedList执行多个操作,包括附加、删除和遍历元素。为了向LinkedList集合中添加元素,我们可以使用各种内置方法,如add()、addFirst()和addLast()。接下来我们将探讨如何使用这些方法向LinkedList添加元素。

在Java中向LinkedList添加元素

在Java中,LinkedList类提供了以下内置方法来添加元素 –

  • add() - 用于在集合末尾插入元素。我们更频繁地使用它而不是其他方法。

  • addFirst() - 该方法用于在LinkedList的第一个索引处插入元素。

  • addLast() - 将元素插入到指定LinkedList的最后一个索引处。

  • addAll() - 当我们需要将一个集合的所有元素都添加到另一个LinkedList中时,我们使用addAll()方法。

在我们的Java程序中,向LinkedList插入元素时,我们只需要将所需的元素作为参数传递即可。

在跳转到向LinkedList添加元素的Java程序之前,让我们讨论一下创建LinkedList类实例的语法。

语法

LinkedList<Type> nameOfinstance = new LinkedList<>();

在这里,’Type’可以是任何包装类。

示例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]

示例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()。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程