Home  >  Article  >  Java  >  Java Example - Get the first and last element of a linked list (LinkedList)

Java Example - Get the first and last element of a linked list (LinkedList)

黄舟
黄舟Original
2017-01-22 16:16:052842browse

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 (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn