Home Java Example Java Util Java List Iterator Example

Related Tutorials


 
 

Share on Google+Share on Google+

Java List Iterator Example

Advertisement
In this tutorial you will learn how to iterate data from a list

Java List Iterator Example

In Java Collection framework every classes provides the iterator() method, that returns the objects of iterator which used to start the collection. By using this iterator objects you can access each element in a collection.

An example given below

At fist make an object and add some data into it as

// Declaring ArrayList object of type String
List myList = new ArrayList();

Then add some elements of type String into it as

// Adding elements to the list
		
myList.add("Rohan");
myList.add("Johan");
myList.add("Sams");
myList.add("Ram");
myList.add("Ramesh");
myList.add("Andrew");

Now call the iterator() method of list and store the result into the Iterator object as

/********** Iteration **********/
Iterator iterator = myList.iterator();

This method returns the iterator object which is used to start the collection

To access all the elements stored in a list apply the loop as

while (iterator.hasNext()) {
		System.out.println(iterator.next());
	}

Please note the method used above of iterator objects, hasNext() and next()

The hasNext() method returns true if any element present in the loop and the next() method returns the next element from the collection

There are more methods present in the iterator interface, to read them please visit the Sun or Oractle site


When you run this application it will display message as shown below:


Rohan
Johan
Sams
Ram
Ramesh
Andrew

Download Select Source Code

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 4, 2011

Related Tutorials

Discuss: Java List Iterator Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:2
lolo
November 30, 2011
I want answer for this Q.

The objective of this assignment is to implement a Car class. First, create a new project in your IDE, call it CarPro, and add a class called Car. The Car class should have the following instance variables: int nsLocation; //distance North or South (North is positive) int ewLocation; // distance East or West ( East is positive) int orientation; // 0=N, 1=E, 2=S, 3=W It should have a constructor that takes an orientation, a nsLocation and an ewLocation. The only public behaviours (methods) that it should have are to travel, turnRight, turnLeft, showLocation and getNsLocation. void travel (int distance) // distance in kms to travel(doesnâ??t change orientation of Car) void turnRight() // turn car 90 degrees to the right void turnLeft() // turn car 90 degrees to the left void showLocation() // show (print to the screen) the current nsLocation, ewLocation // and orientation of the Car int getNsLocation() // return the current position of the Car in the North/South axis. Next, to test your class Car, create another class called CarDriver that contains a main method. The code in the main method of CarDriver class should create two Car objects, one called honda and one called toyota. The toyota should start at location 1 North, 2 East and facing South. The honda should start at location 0 North, 0 East and facing West. The main method of CarDriver should contain also these car navigation instructions: For (int i=0;i<3;i++){ honda.travel(i+2); honda.turnRight(); toyota.turnLeft(); toyota.travel(i+ honda.getNsLocation()); The main method should print out the final details of both carâ??s locations and orientations. At what location and facing in what direction do the Cars end up? Your code should provide data hiding and only allow the car object behaviors that are given above. Finally, you must submit a compressed file of your project including the source code on Blackboard. Also you will demonstrate your work by running your program in the lab.
gaurav
January 7, 2012
java

how to set the value of data base in jtable
DMCA.com