8:30 pm - Thursday April 27, 2017

List Iterator example program is to iterate the elements in arraylist with specified position

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

List Iterator example program is to iterate the elements in arraylist with specified position

Example Program


import java.util.*;

public class LinkedListDemo {
	public static void main(String[] args) {

		// create a LinkedList
		LinkedList list = new LinkedList();

		// add some elements
		list.add("gowtham");
		list.add(2);
		list.add("narendar");
		list.add("10");

		// print the list
		System.out.println("LinkedList:" + list);

		// set Iterator at specified index
		Iterator x = list.listIterator(1);

		// print list with the iterator
		while (x.hasNext()) {
			System.out.println(x.next());
		}
	}
}

 

Output

 

LinkedList:[Hello, 2, Chocolate, 10]

2

Chocolate

10

 

Description

 

The java.util.LinkedList.listIterator(int index) method returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.

 

Declaration

 

Following is the declaration for java.util.LinkedList.listIterator() method

public ListIterator<E> listIterator(int index)

 

Parameters

 

  • index — index of the first element to be returned from the list-iterator

 

Return Value

 

This method returns a ListIterator of the elements in this list (in proper sequence), starting at the specified position in the list

 

Exception

 

  • IndexOutOfBoundsException — if the index is out of range

 

BACK

 

Filed in: Z-A All Java Codes

Comments are closed.