How to Call LinkedList Methods on Objects in Java


This tutorial would tell us how to call push() and pop() methods on LinkedList objects.

Source Code

package com.beginner.examples;

import java.util.LinkedList;

public class PushPopExample {
 
    public static void main(String a[]){
         
        LinkedList ll = new LinkedList();
        ll.add("1");
        ll.add("2");
        System.out.println(ll);
        //push an element
        ll.push("3");
        System.out.println("After push:");
        System.out.println(ll);
        //pop an element
        ll.pop();
        System.out.println("After pop:");
        System.out.println(ll);
    }
}

Output:

[1, 2]
After push:
[3, 1, 2]
After pop:
[1, 2]

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments