Home » Java programming language

Java ArrayDeque remove() Method with Example

ArrayDeque Class remove() method: Here, we are going to learn about the remove() method of ArrayDeque Class with its syntax and example.
Submitted by Preeti Jain, on January 17, 2020

ArrayDeque Class remove() method

Syntax:

    public boolean remove(Object obj);
    public T remove();
  • remove() method is available in java.lang package.
  • remove() method is used to return the head element but with removing the head element from this deque.
  • remove(Object obj) method is used to remove the given object from this deque.
  • remove() method may throw an exception at the time of removing an element from this deque.
    NoSuchElementException: This exception may throw when this deque is "blank".
  • remove(Object obj) method does not throw an exception at the time of removing an element from this deque.
  • These are non-static methods, it is accessible with the class object only and, if we try to access these methods with the class name then we will get an error.

Parameter(s):

  • In the first case, remove(Object obj) - This parameter represents the element to be removed from the deque.
  • In the second case, remove() - This method accepts none parameters.

Return value:

In the first case, the return type of this method is boolean, it returns true when the given element is removed successfully.

In the second case, the return type of this method is T, it removes the head element with returning the element of this deque.

Example:

// Java program to demonstrate the example 
// of remove() method of ArrayDeque

import java.util.*;

public class RemoveOfArrayDeque {
    public static void main(String[] args) {
        // Creating an ArrayDeque with initial capacity of
        // storing elements
        Deque < String > d_queue = new ArrayDeque < String > (10);

        // By using add() method to add elements
        // in ArrayDeque
        d_queue.add("C");
        d_queue.add("C++");
        d_queue.add("Java");
        d_queue.add("Php");
        d_queue.add("DotNet");

        // Display Deque Elements
        System.out.println("d_queue before remove(): ");
        System.out.println("ArrayDeque Elements = " + d_queue);

        // By using remove() method is to remove the 
        // first element from ArrayDeque
        String ele = d_queue.remove();

        System.out.println();

        // Display Deque Elements
        System.out.println("d_queue after remove() : ");
        System.out.println(" d_queue.remove() = " + d_queue);

        // By using remove(Object) method is to remove the 
        // given object from ArrayDeque
        boolean b = d_queue.remove("Java");

        System.out.println();

        // Display Deque Elements
        System.out.println("d_queue after remove(Object) : ");
        System.out.println(" d_queue.remove(object) = " + d_queue);
    }
}

Output

d_queue before remove(): 
ArrayDeque Elements = [C, C++, Java, Php, DotNet]

d_queue after remove() : 
 d_queue.remove() = [C++, Java, Php, DotNet]

d_queue after remove(Object) : 
 d_queue.remove(object) = [C++, Php, DotNet]





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.