从Java中的LinkedList检索最后一个元素
可以使用java.util.LinkedList.getLast()方法分别检索链表的最后一个元素。此方法不需要任何参数,它返回LinkedList的最后一个元素。
演示此的程序如下所示-
示例
import java.util.LinkedList;
public class Demo {
public static void main(String[] args) {
LinkedList<String> l = new LinkedList<String>();
l.add("Andy");
l.add("Sara");
l.add("James");
l.add("Betty");
l.add("Bruce");
System.out.println("The last element of the Linked List is : " + l.getLast());
}
}输出结果
The last element of the Linked List is : Bruce
现在让我们了解上面的程序。
LinkedListl被创建。LinkedList.add()用于将元素添加到LinkedList。然后,LinkedList.getLast()用于检索链表的最后一个元素。演示这的代码片段如下-
LinkedList<String> l = new LinkedList<String>();
l.add("Andy");
l.add("Sara");
l.add("James");
l.add("Betty");
l.add("Bruce");
System.out.println("The last element of the Linked List is : " + l.getLast());热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短