GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java ArrayDeque push() Method
Selected Reading
Java ArrayDeque push() MethodDescriptionThe Java ArrayDeque push(E e) method pushes an element E onto the stack represented by this deque. Essentially it add the element at the front of the ArrayDeque object. DeclarationFollowing is the declaration for java.util.ArrayDeque.push() method public void push(E e) Parameterse − The element to be pushed in the deque. Return ValueThis method does not return any value. ExceptionNullPointerException − if the specified element is null. Pushing an Element to an ArrayDeque of Integers ExampleThe following example shows the usage of Java ArrayDeque push(E) method. In this example, we're using integers. As first, we'll add some items to the deque using add() method and then using push() method, we'll add elements to the stack. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired. 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
Deque<Integer> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add(4);
deque.add(5);
deque.add(6);
// use push() method to add element at the front of the deque
deque.push(3);
deque.push(2);
deque.push(1);//now, element 1 will be at the front
// these elements will be added in continuation with deque.add(6)
deque.add(7);
deque.add(8);
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [1, 2, 3, 4, 5, 6, 7, 8] Pushing an Element to an ArrayDeque of Strings ExampleThe following example shows the usage of Java ArrayDeque push(E) method. In this example, we're using strings. As first, we'll add some items to the deque using add() method and then using push() method, we'll add elements to the stack. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired. 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
Deque<String> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add("D");
deque.add("E");
deque.add("F");
// use push() method to add element to the stack
deque.push("C");
deque.push("B");
deque.push("A");//now, element A will be at the front
// these elements will be added in continuation with deque.add("F")
deque.add("G");
deque.add("H");
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [A, B, C, D, E, F, G, H] Pushing an Element to an ArrayDeque of Objects ExampleThe following example shows the usage of Java ArrayDeque push(E) method. In this example, we're using Student objects. As first, we'll add some items to the deque using add() method and then using push() method, we'll add elements to the front of the deque. Then we're adding more elements using add() method again and the print the arraydeque to check if insertions in the deque are in the order we desired. 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
Deque<Student> deque = new ArrayDeque<>();
// use add() method to add elements in the deque
deque.add(new Student(4, "Julie"));
deque.add(new Student(5, "Robert"));
deque.add(new Student(6, "Adam"));
// use push() method to add element to the stack
deque.push(new Student(3, "Rohan"));
deque.push(new Student(2, "Sohan"));
deque.push(new Student(1, "Mohan"));//now, Student 1 will be at the front
// these elements will be added in continuation with deque.add(new Student(6, "Adam"))
deque.add(new Student(7, "Ali"));
deque.add(new Student(8, "Ahmad"));
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [[ 1, Mohan ], [ 2, Sohan ], [ 3, Rohan ], [ 4, Julie ], [ 5, Robert ], [ 6, Adam ], [ 7, Ali ], [ 8, Ahmad ]] java_util_arraydeque.htm
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |