从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 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短