Java Java.util.LinkedList.peek(), peekfirst(), peeklast()方法
链接列表类提供了 “查看 “列表中第一个和最后一个元素的功能,因此在 只需要检索 而不一定需要删除的情况下是非常有用的。有三种功能,本文将对这些功能进行讨论。
1. peek() : 这个方法 检索,但不删除 ,这个列表的头部(第一个元素)。
声明:
public E peek()
返回值 :
该方法返回该列表的头部,如果该列表为空,则返回空。
// Java code to demonstrate the working
// of peek() in LinkedList
import java.util.*;
public class LinkedPeek1 {
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 list is :" + list);
// peek at the head of the list
// Prints 1st element, "Geeks"
System.out.println("Head of the list : " + list.peek());
}
}
输出:
The initial list is :[Geeks, 4, Geeks, 8]
Head of the list : Geeks
2. peekFirst() : 这个方法 检索,但不删除 ,这个列表的第一个元素,如果这个列表是空的,则返回null。这个方法的作用类似于 peek()。
**Declaration :**
public E peekFirst()
**Return Value :**
This method returns the first element of this list, or null if this list is empty
// Java code to demonstrate the working
// of peekFirst() in LinkedList
import java.util.*;
public class LinkedPeek2 {
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 list is :" + list);
// peek at the first element of the list
// Prints 1st element, "Geeks"
System.out.println("First element of the list is : " + list.peekFirst());
}
}
输出:
The initial list is :[Geeks, 4, Geeks, 8]
First element of the list is : Geeks
3. peekLast() : 该方法 检索 ,但不删除该列表的 最后一个元素 ,如果该列表为空,则返回null。
声明
public E peekLast()
返回值
该方法返回该列表的最后一个元素,如果该列表是空的,则返回空。
// Java code to demonstrate the working
// of peekLast() in LinkedList
import java.util.*;
public class LinkedPeek3 {
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 list is :" + list);
// peek at the last element of the list
// Prints last element, 8
System.out.println("Last element of the list is : " + list.peekLast());
}
}
输出:
The initial list is :[Geeks, 4, Geeks, 8]
Last element of the list is : 8
实际应用: 可以想到的实际应用是,这可以用在潜在的纸牌游戏中, 个人可以 在询问他们想看的元素时 偷看牌中的第一个或最后一个元素 。下面的代码解释了这个工作。
// Java code to demonstrate the application
// of peek()
import java.util.*;
public class LinkedPeekApp {
public static void main(String[] args)
{
// declaring a LinkedList
LinkedList list = new LinkedList();
// adding elements in deck
list.add(5);
list.add(4);
list.add("Jack");
list.add(8);
list.add("King");
// printing the list
System.out.println("The initial deck is :" + list);
String d = "upper";
System.out.println("The element chosen to peek is : " + d);
if (d == "upper")
System.out.println("The Upper element is : " + list.peekFirst());
else
System.out.println("The Lower element is : " + list.peekLast());
}
}
输出:
The initial deck is :[5, 4, Jack, 8, King]
The element chosen to peek is : upper
The Upper element is : 5
极客教程