Java LinkedList offerFirst(E) 方法



描述

Java LinkedList offerFirst(E e) 方法将指定的元素 E 插入到此 linkedList 的开头。它类似于 addFirst() 方法。

声明

以下是 java.util.LinkedList.offerFirst() 方法的声明

public boolean offerFirst(E e)

参数

e − 要添加到开头的元素。

返回值

如果元素已添加到此 linkedList,则此方法返回 true,否则返回 false

异常

NullPointerException − 如果指定的元素为 null

将元素插入到整数 LinkedList 开头的示例

以下示例显示了 Java LinkedList offerFirst(E) 方法的使用。在此示例中,我们使用整数。首先,我们将使用 add() 方法向 linkedList 添加一些项,然后使用 offerFirst() 方法将元素添加到 linkedList 的开头。然后,我们再次使用 add() 方法添加更多元素,并打印数组以检查 linkedList 中的插入是否按我们期望的顺序进行。

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linkedList
      LinkedList<Integer> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(4);
      linkedList.add(5);
      linkedList.add(6);

      // use offerFirst() method to add element at the front of the linkedList
      linkedList.offerFirst(3);
      linkedList.offerFirst(2);
      linkedList.offerFirst(1);//now, element 1 will be at the front

      // these elements will be added in continuation with linkedList.add(6)
      linkedList.add(7);
      linkedList.add(8);

      // let us print all the elements available in linkedList
      System.out.println("LinkedList = " + linkedList);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

LinkedList = [1, 2, 3, 4, 5, 6, 7, 8]

将元素插入到字符串 LinkedList 开头的示例

以下示例显示了 Java LinkedList offerFirst(E) 方法的使用。在此示例中,我们使用字符串。首先,我们将使用 add() 方法向 linkedList 添加一些项,然后使用 offerFirst() 方法将元素添加到 linkedList 的开头。然后,我们再次使用 add() 方法添加更多元素,并打印数组以检查 linkedList 中的插入是否按我们期望的顺序进行。

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linkedList
      LinkedList<String> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add("D");
      linkedList.add("E");
      linkedList.add("F");

      // use offerFirst() method to add element at the front of the linkedList
      linkedList.offerFirst("C");
      linkedList.offerFirst("B");
      linkedList.offerFirst("A");//now, element A will be at the front

      // these elements will be added in continuation with linkedList.add("F")
      linkedList.add("G");
      linkedList.add("H");

      // let us print all the elements available in linkedList
      System.out.println("LinkedList = " + linkedList);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

LinkedList = [A, B, C, D, E, F, G, H]

将元素插入到对象 LinkedList 开头的示例

以下示例显示了 Java LinkedList offerFirst(E) 方法的使用。在此示例中,我们使用 Student 对象。首先,我们将使用 add() 方法向 linkedList 添加一些项,然后使用 offerFirst() 方法将元素添加到 linkedList 的开头。然后,我们再次使用 add() 方法添加更多元素,并打印数组以检查 linkedList 中的插入是否按我们期望的顺序进行。

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {

      // create an empty linkedList
      LinkedList<Student> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(new Student(4, "Julie"));
      linkedList.add(new Student(5, "Robert"));
      linkedList.add(new Student(6, "Adam"));

      // use offerFirst() method to add element at the front of the linkedList
      linkedList.offerFirst(new Student(3, "Rohan"));
      linkedList.offerFirst(new Student(2, "Sohan"));
      linkedList.offerFirst(new Student(1, "Mohan"));//now, Student 1 will be at the front

      // these elments will be added in continuation with linkedList.add(new Student(6, "Adam"))
      linkedList.add(new Student(7, "Ali"));
      linkedList.add(new Student(8, "Ahmad"));

      // let us print all the elements available in linkedList
      System.out.println("LinkedList = " + linkedList);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

LinkedList = [[ 1, Mohan ], [ 2, Sohan ], [ 3, Rohan ], [ 4, Julie ], [ 5, Robert ], [ 6, Adam ], [ 7, Ali ], [ 8, Ahmad ]]
java_util_linkedlist.htm
广告

© . All rights reserved.