The following example demonstrates how to use linkedlistname.getFirst() and linkedlistname.getLast() of the LinkedList class to obtain the first and last elements of the linked list:
/* author by w3cschool.cc Main.java */import java.util.LinkedList;public class Main { public static void main(String[] args) { LinkedList lList = new LinkedList(); lList.add("100"); lList.add("200"); lList.add("300"); lList.add("400"); lList.add("500"); System.out.println("链表的第一个元素是:" + lList.getFirst()); System.out.println("链表的第二个元素是:" + lList.getLast()); }}
The output result of running the above code is:
链表的第一个元素是:100 链表的第二个元素是:500
The above is Java example - Get the contents of the first and last elements of the linked list (LinkedList). For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!