pcs
pcs

Reputation: 1854

Explanation in arraydeque push method in java

I am new to java, Im learning from oracle docs.

So now i am getting started util package and corresponding classes and interfaces.

According to ArrayDeque class,I want know, how to use push method, foe that i refered this http://www.tutorialspoint.com/java/util/arraydeque_push.htm.

In this i just confused how to get output..

And also,

import java.util.ArrayDeque; import java.util.Deque;

Here ArrayDeque is class and Deque is interface,.. is this way i should write for every time when use other methods of ArrayDeque class?

Or

May i use import java.util.*; instead of using import java.util.ArrayDeque; and import java util.Deque; ?

PROGRAM:

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {

   // create an empty array deque with an initial capacity
   Deque<Integer> deque = new ArrayDeque<Integer>(8);

   // use add() method to add elements in the deque
   deque.add(25);
   deque.add(30);
   deque.add(35);

   // adding elements using push() method
   deque.push(10);
   deque.push(15);

   System.out.println("Printing Elements after using push operation:");
   for (Integer number : deque) {
   System.out.println("Number = " + number);
   }
   }
}

And output is:

Number = 25
Number = 30
Number = 35
Printing Elements after using push operation:
Number = 15
Number = 10
Number = 25
Number = 30
Number = 35

Anybody guide me , working of this method, if i got a idea means.. i m sure i will workout other methods and all easily.

Thanks,

Upvotes: 0

Views: 696

Answers (2)

Programmer
Programmer

Reputation: 453

The push method is used to Push an item onto the top of the stack.

Another Defination

The push method adds an element to the stack. It takes as its argument the Object to be pushed onto the stack.

Example: http://www.brpreiss.com/books/opus5/html/page135.html

Upvotes: 1

Aaron Digulla
Aaron Digulla

Reputation: 328594

The documentation at tutorialspoint is a bit flimsy. Look at the official documentation to understand what the API does: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html

specifically https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#push-E-

Pushes an element onto the stack represented by this deque (in other words, at the head of this deque)

So add() adds the elements at the end of the queue, push() adds them at the front.

Upvotes: 0

Related Questions