0

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,

5
  • Your question is very unclear. Are you asking why the output is what it is, or whether you should refer to an object via an interface rather than the concrete class? Please edit your question to be much clearer. Commented Jun 4, 2015 at 9:27
  • @Jon skeet: i m not asking why.. i need explanation about how get this output? Thanks.. Commented Jun 4, 2015 at 9:32
  • But you've said the output is that - if it isn't actually that now, what is it? Your comment has only added more confusion, rather than clearing it up... Commented Jun 4, 2015 at 9:33
  • Sir.. i need some explanation about this program and also about how to import class and interface in the beginning of the program sir.. and i got a solution from the below answers sir.. thanks again. Commented Jun 4, 2015 at 9:47
  • "i need some explanation about this program" is hopelessly vague. You need to ask specific questions. Commented Jun 4, 2015 at 9:48

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

@saina: Do you know what a "stack" is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.