从Java LinkedList获取第一个和最后一个元素
可以分别使用方法java.util.LinkedList.getFirst()和java.util.LinkedList.getLast()获得链接列表的第一个和最后一个元素。这些方法都不要求任何参数。
演示此程序如下
示例
import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
LinkedList l = new LinkedList();
l.add("John");
l.add("Sara");
l.add("Susan");
l.add("Betty");
l.add("Nathan");
System.out.println("The first element of the Linked List is : " + l.getFirst());
System.out.println("The last element of the Linked List is : " + l.getLast());
}
}上面程序的输出如下
The first element of the Linked List is : John The last element of the Linked List is : Nathan
现在让我们了解上面的程序。
LinkedListl被创建。然后,使用LinkedList.getFirst()和LinkedList.getLast()分别获取链接列表的第一个和最后一个元素。演示这的代码片段如下
LinkedList l = new LinkedList();
l.add("John");
l.add("Sara");
l.add("Susan");
l.add("Betty");
l.add("Nathan");
System.out.println("The first element of the Linked List is : " + l.getFirst());
System.out.println("The last element of the Linked List is : " + l.getLast());热门推荐
10 毕业祝福语简短给同学
11 演讲结尾祝福语简短英文
12 高温早安祝福语简短精辟
13 今年节日祝福语简短
14 高考祝福语 简短12字
15 毕业创意蛋糕祝福语简短
16 友情赠送蛋糕祝福语简短
17 经典祝福语简短情侣句子
18 新年祝同学祝福语简短