0% found this document useful (0 votes)
59 views3 pages

Accessing A Java Collection Via A Iterator

The document discusses how to access elements in a Java collection using an Iterator. It provides 3 steps: 1) create an Iterator object from the collection, 2) use hasNext() in a loop to check for next elements, 3) use next() in the loop to access each element. The Iterator interface provides methods like iterator(), hasNext(), and next() to traverse collections forward. The ListIterator interface extends Iterator to also allow traversal backward, providing additional methods like hasPrevious() and previous().

Uploaded by

mahadev shiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Accessing A Java Collection Via A Iterator

The document discusses how to access elements in a Java collection using an Iterator. It provides 3 steps: 1) create an Iterator object from the collection, 2) use hasNext() in a loop to check for next elements, 3) use next() in the loop to access each element. The Iterator interface provides methods like iterator(), hasNext(), and next() to traverse collections forward. The ListIterator interface extends Iterator to also allow traversal backward, providing additional methods like hasPrevious() and previous().

Uploaded by

mahadev shiva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Accessing a Java Collection via a Iterator

The java collection framework often we want to cycle through the elements. For example, we might want to display

each element of a collection. The java provides an interface Iterator that is available inside the java.util package to

cycle through each element of a collection.

We use the following steps to access a collection of elements using the Iterator.

 Step - 1: Create an object of the Iterator by calling collection.itertor( ) method.

 Step - 2: Use the method hasNext( ) to access to check does the collection has the next element. (Use a

loop).

 Step - 3: Use the method next( ) to access each element from the collection. (use inside the loop).

The Iterator has the following methods.

Method Description

Iterator iterator( ) Used to obtain an iterator to the start of the collection.

boolean hasNext( ) Returns true if the collection has the next element, otherwise, it returns false.

E next( ) Returns the next element available in the collection.

class Demo

public static void main(String[] args)

ArrayList< String> ar = new ArrayList< String>();

ar.add("ab");

ar.add("bc");
ar.add("cd");

ar.add("de");

Iterator it = ar.iterator(); //Declaring Iterator

while(it.hasNext())

System.out.print(it.next()+" ");

Accessing elements using ListIterator


The ListIterator interface is used to traverse through a list in both forward and backward directions. It does not
support all types of collections. It supports only the collection which implements the List interface.
The ListIterator provides the following methods to traverse through a list of elements.

Method Description

ListIterator Used obtain an iterator of the list collection.


listIterator( )

boolean hasNext( ) Returns true if the list collection has next element, otherwise it returns false.

E next( ) Returns the next element available in the list collection.

boolean Returns true if the list collection has previous element, otherwise it returns false.
hasPrevious( )

E previous( ) Returns the previous element available in the list collection.

int nextIndex( ) Returns the index of the next element. If there is no next element, returns the size of
the list.

E previousIndex( ) Returns the index of the previous element. If there is no previous element, returns -1.
{

public static void main(String[] args)

ArrayList< String> ar = new ArrayList< String>();

ar.add("ab");

ar.add("bc");

ar.add("cd");

ar.add("de");

ListIterator litr = ar.listIterator();

while(litr.hasNext()) //In forward direction

System.out.print(litr.next()+" ");

while(litr.hasPrevious()) //In backward direction

System.out.print(litr.previous()+" ");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy