Home > Java > javaTutorial > 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)

黄舟
Release: 2017-01-22 16:16:05
Original
2874 people have browsed it

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());
   }}
Copy after login

The output result of running the above code is:

链表的第一个元素是:100
链表的第二个元素是:500
Copy after login

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)!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template