Home » Java programming language

Java LinkedList void push(Object o) method with Example

Java LinkedList void push(Object o) method: Here, we are going to learn about the void push(Object o) method of LinkedList class with its syntax and example.
Submitted by Preeti Jain, on June 24, 2019

LinkedList void push(Object o) method

  • This method is available in package java.util.LinkedList.push(Object o).
  • This method is used to insert or push an object at the top of the stack(initial or first position) represented by the linked list.
  • This method is equivalent to addFirst(Object o) of the LinkedList.

Syntax:

    void push(Object o){
    }

Parameter(s):

We can pass only one object as a parameter in the method and that object will add at the beginning or top of the linked list.

Return value:

The return type of this method is void that means this method returns nothing.

Java program to demonstrate example of LinkedList push() method

import java.util.LinkedList;

public class LinkList {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        // use add() method to add elements in the list 
        list.add("J");
        list.add("A");
        list.add("V");
        list.add("A");

        //  Current list Output
        System.out.println("The Current list is:" + list);

        // Add new elements at the beginning or top of the list
        list.push("PROGRAMMING");

        //  New list Output after implementing push(Object o)
        System.out.println("The new List is:" + list);
    }
}

Output

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The Current list is:[J, A, V, A]
The new List is:[PROGRAMMING, J, A, V, A]



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.