Menu
Topics Index
...
`

Iterate list using iterator


If you need explanation Read this topic

If you need Answer Take test on this topic

Iterate list using iterator
What will be the output of the following program?
import java.util.*;

public class ListIterator
{
    public static void main(String args[])
    {
        List<Integer> list = new ArrayList<Integer>();
        java.util.ListIterator<Integer> listIterator = null;
        list.add(23);
        list.add(98);
        list.add(29);
        list.add(71);
        list.add(5);
        listIterator = list.listIterator();
        System.out.println("Elements in backward direction:");
        while(listIterator.hasPrevious())
        {
            System.out.println(listIterator.previous());
        }
    }
}

A. Elements in backward direction: 
5
71
29
98
23
B. Elements in backward direction: 
23
98
29
71
5
C. Elements in backward direction:
5
23
29
71
98
D. Elements in backward direction:
E. Compilation Error or Runtime Error
Topic: List Iterator In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Answer is D...here we are trying to add elements to a list and though we have got a reference to listiterator

Iterator enables you to cycle through a collection, obtaining or removing elements. ListIterator extends Iterator to allow bidirectional traversal of a list, and the modification of elements.

Before you can access a collection through an iterator, you must obtain one. Each of the collection classes provides an iterator( ) method that returns an iterator to the start of the collection. By using this iterator object, you can access each element in the collection, one element at a time.

Returns true if there is a previous element. Otherwise, returns false
Since we listiterator points to the beginning of collection,and there is nothing before to the first element and haspreviuos() returns false
So the control never enters the while loop

Posted by Shashanka Mogaliraju    2014-12-02 12:35:26


ans d.

List Extends Collection to handle sequences (lists of objects).

The ArrayList class extends AbstractList and implements the List interfaceArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are
of a fixed length. After arrays are created, they cannot grow or shrink, which means that you
must know in advance how many elements an array will hold. But, sometimes, you may not
know until run time precisely how large an array you need. To handle this situation, the
Collections Framework defines ArrayList. In essence, an ArrayList is a variable-length array
of object references. That is, an ArrayList can dynamically increase or decrease in size. Arraylists are created with an initial size. When this size is exceeded, the collection is automatically
enlarged. When objects are removed, the array can be shrunk.

ListIterator<E> listIterator( ) Returns an iterator to the start of the invoking list.

boolean hasPrevious( ) Returns true if there is a previous element. Otherwise, returns false.

coming to this program here elements are added in the list by using add() method.iterator list invoked.
sop prints "Element in backward direction"

iteration always starts at element here while loop condition check is there any element present before the 1st element.here no such elements are there so condition failed.while loop not get executed.

Posted by Maheshwari Natarajan    2014-12-02 18:36:38


This dose is now closed and the winners are Shashanka Mogaliraju,  for 'First Correct Comment', Maheshwari Natarajan,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Varun Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-12-03 03:10:50


© meritcampus 2019

All Rights Reserved.

Open In App