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 clear() Method
Selected Reading
Java ArrayDeque clear() MethodDescriptionThe Java ArrayDeque clear() method removes all of the elements from this deque. This method is very useful when we want to reuse a queue or reset the queue. DeclarationFollowing is the declaration for java.util.ArrayDeque.clear() method public void clear() ParametersNA Return ValueThis method does not return any value. ExceptionNA Clearing an ArrayDeque of Integers ExampleThe following example shows the usage of Java ArrayDeque clear() method. In this example, we're using Integers. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now. 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(20);
deque.add(30);
deque.add(10);
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
// finding size of this deque
int retval = deque.size();
System.out.println("Deque consists of "+ retval +" elements");
System.out.println("Performing clear operation !!");
deque.clear();
retval = deque.size();
System.out.println("Now, deque consists of "+ retval +" elements");
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [20, 30, 10] Deque consists of 3 elements Performing clear operation !! Now, deque consists of 0 elements Clearing an ArrayDeque of Strings ExampleThe following example shows the usage of Java ArrayDeque clear() method. In this example, we're using Strings. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now. 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("A");
deque.add("B");
deque.add("C");
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
// finding size of this deque
int retval = deque.size();
System.out.println("Deque consists of "+ retval +" elements");
System.out.println("Performing clear operation !!");
deque.clear();
retval = deque.size();
System.out.println("Now, deque consists of "+ retval +" elements");
}
}
OutputLet us compile and run the above program, this will produce the following result − ArrayDeque = [A, B, C] Deque consists of 3 elements Performing clear operation !! Now, deque consists of 0 elements Clearing an ArrayDeque of Objects ExampleThe following example shows the usage of Java ArrayDeque clear() method. In this example, we're using Student objects. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now. 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(1, "Julie"));
deque.add(new Student(2, "Robert"));
deque.add(new Student(3, "Adam"));
// let us print all the elements available in deque
System.out.println("ArrayDeque = " + deque);
// finding size of this deque
int retval = deque.size();
System.out.println("Deque consists of "+ retval +" elements");
System.out.println("Performing clear operation !!");
deque.clear();
retval = deque.size();
System.out.println("Now, deque consists of "+ retval +" elements");
}
}
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, Julie ], [ 2, Robert ], [ 3, Adam ]] Deque consists of 3 elements Performing clear operation !! Now, deque consists of 0 elements 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. |