Home » Java programming language

Java ArrayDeque iterator() Method with Example

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

ArrayDeque Class iterator() method

  • iterator() Method is available in java.lang package.
  • iterator() Method is used to return an iterator over the deque elements.
  • iterator() Method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • iterator() Method does not throw an exception at the time of returning the Iterator.

Syntax:

    public Iterator<T> iterator();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is Iterator<T>, it returns an iterator over the deque elements.

Example:

// Java program to demonstrate the example 
// of Iterator<T> iterator() method of ArrayDeque 

import java.util.*;

public class IteratorOfArrayDeque {
    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");

        System.out.println();

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

        System.out.println();

        // By using iterator() method to iterate the
        // elements in same order as ArrayDeque
        System.out.println("d_queue after iterator(): ");

        // Display Deque Elements
        for (Iterator iter = d_queue.iterator(); iter.hasNext();)
            System.out.println("Deque Elements : " + iter.next());
    }
}

Output


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

d_queue after iterator(): 
Deque Elements : C
Deque Elements : C++
Deque Elements : Java
Deque Elements : Php
Deque Elements : DotNet


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.