Home > Java > javaTutorial > body text

Java Example - Get elements of linked list

黄舟
Release: 2017-01-22 16:20:46
Original
1717 people have browsed it

The following example demonstrates the use of top() and pop() methods to obtain the elements of the linked list:

/*
 author by w3cschool.cc
 Main.java
 */import java.util.*;public class Main {
   private LinkedList list = new LinkedList();
   public void push(Object v) {
      list.addFirst(v);
   }
   public Object top() {
      return list.getFirst();
   }
   public Object pop() {
      return list.removeFirst();
   }
   public static void main(String[] args) {
      Main stack = new Main();
      for (int i = 30; i < 40; i++)
         stack.push(new Integer(i));
      System.out.println(stack.top());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
      System.out.println(stack.pop());
   }}
Copy after login

The output result of running the above code is:

39
39
38
37
Copy after login

The above is the Java example - getting the content of the elements of the linked list, more related Please pay attention to the PHP Chinese website (www.php.cn) for content!

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