Java Java.util.LinkedList.offer(), offerFirst(), offerLast()方法
链接列表也有一个函数,用于 灵活地添加元素 ,并帮助在列表的前部和后部添加,这些函数字面意思是 “提供 “设施,名为offer()。有三种类型,在下面这篇文章中讨论。
offer(E e) : 这个方法将 指定的元素 添加到这个列表的 尾部 (最后一个元素)。
声明:
public boolean offer(E e)
参数 :
e:要添加的元素
返回值 :
该方法返回真
// Java code to demonstrate the working
// of offer(E e) in linked list
import java.util.*;
public class LinkedListoffer1 {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked list is : " + list);
// offering a new element
// adds element at tail.
list.offer("Astha");
// printing the new list
System.out.println("LinkedList after insertion using offer() : " + list);
}
}
输出:
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offer() : [Geeks, 4, Geeks, 8, Astha]
offerFirst(E e) : 这个方法在这个列表的 前面 插入了 指定的元素。
**Declaration :**
public boolean offerFirst(E e)
**Parameters :**
**e :** the element to add
**Return Value :**
This method returns true
// Java code to demonstrate the working
// of offerFirst(E e) in linked list
import java.util.*;
public class LinkedListOfferFirst {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked list is : " + list);
// offering a new element
// adds element at head.
list.offerFirst("Astha");
// printing the new list
System.out.println("LinkedList after insertion using offerFirst() : " + list);
}
}
输出:
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerFirst() : [Astha, Geeks, 4, Geeks, 8]
offerLast(E e) : 这个方法在这个列表的 末尾 插入 指定的元素。
声明:
public boolean offerLast(E e)
参数 :
e:要添加的元素
返回值 :
该方法返回真
// Java code to demonstrate the working
// of offerLast(E e) in linked list
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
// Declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements
list.add("Geeks");
list.add(4);
list.add("Geeks");
list.add(8);
// printing the list
System.out.println("The initial Linked list is : " + list);
// offering a new element
// adds element at end.
list.offerLast("Astha");
// printing the new list
System.out.println("LinkedList after insertion using offerLast() : " + list);
}
}
输出:
The initial Linked list is : [Geeks, 4, Geeks, 8]
LinkedList after insertion using offerLast() : [Geeks, 4, Geeks, 8, Astha]
实际应用: 这些函数的 “灵活添加 “的质量可以在 队列中的优先级添加的 情况下进行,在这种情况下,比阈值大的元素必须在比它小的元素之前被处理。下面的一小段代码讨论了这个问题。
// Java code to demonstrate the application
// of offer() in linked list
import java.util.*;
public class LinkedListOfferLast {
public static void main(String[] args)
{
// Declaring LinkedLists
LinkedList<Integer> list = new LinkedList<Integer>();
LinkedList prioList = new LinkedList();
// adding elements
list.add(12);
list.add(4);
list.add(8);
list.add(10);
list.add(3);
list.add(15);
// declaring threshold
int thres = 10;
// printing the list
System.out.println("The initial Linked list is : " + list);
while (!list.isEmpty()) {
int t = list.poll();
// adding >=10 numbers at front rest at back
if (t >= 10)
prioList.offerFirst(t);
else
prioList.offerLast(t);
}
// The resultant list is
System.out.println("The prioritized Linked list is : " + prioList);
}
}
输出:
The initial Linked list is : [12, 4, 8, 10, 3, 15]
The prioritized Linked list is : [15, 10, 12, 4, 8, 3]
极客教程