Iterate a List in Java

By: Dorris Emailed: 1768 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This tutorial demonstrates the use of ArrayList, Iterator and a List. There are many ways to iterate a list of objects in Java. This sample program shows you the different ways of iterating through a list in Java.

In java a list object can be iterated in following ways:

  • Using Iterator class
  • With the help of for loop
  • With the help of while loop
  • Java 5 for-each loop

Following example code shows how to iterate a list object:

import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
 
public class IterateAList {
  public static void main(String[] argv) {
 
                ArrayList arrJavaTechnologies = new ArrayList();

                arrJavaTechnologies.add("JSP");
                arrJavaTechnologies.add("Servlets");
                arrJavaTechnologies.add("EJB");
                arrJavaTechnologies.add("JDBC");
                arrJavaTechnologies.add("JPA");
                arrJavaTechnologies.add("JSF");


                //Iterate  with the help of Iterator class
                System.out.println("Iterating with the help of Iterator class");
                Iterator iterator = arrJavaTechnologies.iterator();
                while(iterator.hasNext()){
                        System.out.println( iterator.next() );
                }

                //Iterate  with the help of for loop
                System.out.println("Iterating with the help of for loop");
                for (int i=0; i< arrJavaTechnologies.size(); i++)
                {
                        System.out.println( arrJavaTechnologies.get(i) );
                }

                //Iterate  with the help of while loop
                System.out.println("Iterating with the help of while loop");
                int j=0; 
                while (j< arrJavaTechnologies.size())
                {
                        System.out.println( arrJavaTechnologies.get(j) );
                        j++;
                }
                //Iterate  with the help of java 5 for-each loop
                System.out.println("Iterate  with the help of java 5 for-each loop");
                for (String element : arrJavaTechnologies) // or sArray
                {
                        System.out.println( element );
                }
 
  }
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Wow NIce

View Tutorial          By: Zahid ALi at 2015-01-16 22:35:45

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Dorris
sum of all digits in a given number using Shell Script
sh -x and sh -v in Linux
case..esac..in Linux Shell Script
while loop in Linux Shell Script
Nested for loop in Linux Shell Script
for loop in Linux Shell Script
Multilevel if-then-else in Linux Shell Script
Nested if-else-fi in Linux Shell Script
if...else...fi in Linux Shell Script
test command or [ expr ] in Linux Shell Script
if condition in Linux Shell Script
Download Linux. Which linux and where?
Formula in Hibernate
Primary keys assigned by triggers in Hibernate
Assigned identifiers in Hibernate

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
InetAddress Example program in Java
Read from a COM port using Java program
Stack example in Java - push(), pop(), empty(), search()
How to use ArrayList in Java
The Basic Structure of a Simple Java program
Using StringTokenizer in Java
FileReader and FileWriter example program in Java
Transient vs Volatile modifiers in Java
Vector example in Java
Method Overriding in Java
Method Overloading (function overloading) in Java
instanceof sample program in Java
append() in Java
Student marks calculation program in Java
Reading from a file and writing to a file using Java program
Most Emailed Articles (in Java)
Text to Speech conversion program in Java
inheritance in Java
How to initialize an Array and how to copy the array
Using Charts in JasperReports
What is Unicode?
Operator Precedence in Java
Of Object, equals (), == and hashCode ()
DateFormat sample program in Java
A Simple whois program in Java
PushbackReader sample program in Java
CharArrayReader example program in Java
ByteArrayOutputStream - sample program in Java
Vector example in Java
Arrays example in Java - asList(), binarySearch(), fill(), sort(), equals()
HashSet Sample program in Java