Online Training Spring Hibernate Training in Delhi R Programming Training Course Big Data and Hadoop Training Java Training

Java List Iterator Example

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

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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

Share on Google+Share on Google+

Java List Iterator Example

Posted on: November 4, 2011 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

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

Ads

 

Ads