从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 婆婆高寿祝福语大全简短