Iterate a List in Java
Posted on: September 28, 2010 at 12:00 AM
In this tutorial I will show you how you can iterate a list in your Java program.

Iterate a List in Java

In this tutorial I will show you how you can iterate a list in your Java program. In this tutorial we are creating a list of Java technologies and then iterating the list and printing the data on console.

Ways to iterate 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 );
		}
 
  }
}

Output of the program:


C:\techindex\techindex>java IterateAList
Iterating with the help of Iterator class
JSP
Servlets
EJB
JDBC
JPA
JSF
Iterating with the help of for loop
JSP
Servlets
EJB
JDBC
JPA
JSF
Iterating with the help of while loop
JSP
Servlets
EJB
JDBC
JPA
JSF
Iterate with the help of java 5 for-each loop
JSP
Servlets
EJB
JDBC
JPA
JSF

C:\techindex\techindex>

In this tutorial we have developed Java program to Iterate a list.

More Tutorials on roseindia.net for the topic Iterate a List in Java.
Iterate a List in Java
Iterate a List in Java In this tutorial I will show you how you can iterate a list in your Java program. In this tutorial we are creating a list of Java... to iterate a List in Java In java a list object can be iterated in following
 
Iterate list of objects on jsp page
Iterate list of objects on jsp page  Hi, I want to iterate the list of objects on jsp page using custom tags. Can you please give me one link or example? Thanks
 
Collection Iterate Example
Collection Iterate Example     ... of interface Iterator in Java. The description of the code is given below... the hasNext() method through the following java program. True is return
 
how to iterate hibernate object
how to iterate hibernate object  am just learning hibernate .by the hibernate am getting one list to display a grid(grid has 5 column). now i want to iterate one only column value to calculate total of that particular column. how
 
Java List Iterator Example
Java List Iterator Example In Java Collection framework every classes provides... List myList = new ArrayList(); Then add some elements of type String into it as // Adding elements to the list myList.add("Rohan"); myList.add
 
Iterate java Arraylist
(). Example of Java Arraylist Iterate import java.util.*; public class javaarray { public static void main(String[] args) { List list = new... is a collection class and implements the List Inteface. All the elements
 
How to handle the list dynamically in struts inside logic:iterate tag
How to handle the list dynamically in struts inside logic:iterate tag ...:iterate name="loginForm" id="inutParams" property="inutParams" indexId="i"> <...; In my action I wish to take the values from list named inutParamsvalues
 
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
 
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
 
how to iterate a map in java using for each
how to iterate a map in java using for each  Hi, I have a HashMap in Java and there is need to iterate all the items. how to iterate a map in java... of Java 8: import java.util.HashMap; import java.util.Map; public class MapIterate
 
List In Java
List In Java In this section we will read about the List data structure in Java and how the List data structure can be implemented in Java. List data structure is implemented in Java using List interface. List interface allows
 
Logic Iterate.
Logic Iterate.  I have one doubt about logic:iterate. I use text feild inside of logic:iterate , there is possible will generate N no of rows (text feild) inside of iterate. my doubt is how to get the Paritcular rows text
 
Logic Iterate.
Logic Iterate.  logic iterate Struts I have one doubt about logic:iterate. I use text field inside of logic:iterate , there is possible will generate N no of rows (text field) inside of iterate. my doubt is how to get
 
Iterating java arrays
Iterating java Arrays arrays can be iterated by the for, for each loop. array elements can be added to the list and then it can...};      List list=new ArrayList();               for (int i = 0; i < ar.length; i
 
logic-iterate

 
Iterate Tag used with a Map.
Iterate Tag used with a Map.  How Iterate Tag used with a Map
 
ModuleNotFoundError: No module named 'iterate'
ModuleNotFoundError: No module named 'iterate'  Hi, My Python... 'iterate' How to remove the ModuleNotFoundError: No module named 'iterate... to install padas library. You can install iterate python with following command
 
ModuleNotFoundError: No module named 'iterate'
ModuleNotFoundError: No module named 'iterate'  Hi, My Python... 'iterate' How to remove the ModuleNotFoundError: No module named 'iterate... to install padas library. You can install iterate python with following command
 
Java Map Iterate
the entrySet() method.It returns the data as Set Example of Java Map Iterate import java.util.*; public class map1 { public static void main
 
logic iterate tag - Struts
logic iterate tag  Hey I am displaying the values in a jsp called categoryView.jsp using logic iterate tag as Now properties bname and quant belongs to another formbean named as ViewCategoryForm.java.When I
 
JavaScript Iterate JOSN array.
JavaScript Iterate JOSN array.  Hi, I have key value pair of key value in JSON object. How to iterate it? Thanks   Hi, Here is example...;body> <p>JavaScript Iterate JOSN array.</p> </body> </html>
 
JavaScript Iterate JOSN array.
JavaScript Iterate JOSN array.  Hi, I have key value pair of key value in JSON object. How to iterate it? Thanks   Hi, Here is example...;body> <p>JavaScript Iterate JOSN array.</p> </body> </html>
 
JavaScript Iterate JOSN array.
JavaScript Iterate JOSN array.  Hi, I have key value pair of key value in JSON object. How to iterate it? Thanks   Hi, Here is example...;body> <p>JavaScript Iterate JOSN array.</p> </body> </html>
 
Struts logic iterate
iterate <%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>...; <body> <h1> Search </h1> <logic:iterate name...;tr> <td align="center"> <logic:iterate name="listMs
 
struts logic:iterate tag
struts logic:iterate tag  Hi All, I am writing a look up jsp which is using logc:iterate every thing is fine except this the problem here is unable to go inside the tag. Here is the stack trace I am getting. [#|2010-10-27T00
 
Iterate java collection

 
- Struts

 
Java list
Java list  What is the difference between a Choice and a List
 
How to iterate over a checkbox group
How to iterate over a checkbox group  Hi. I am new to using JQuery and would much appreciate your help. I have a form on which I've created 5..." groups. My plan of attack was to iterate over the MASTER checkbox group
 
Linked list
Linked list  what is difference btw linked list in datastructure and linked list in java
 
Java Util Examples List
Java Util Examples List - Util Tutorials   ... In this Example you will learn how to iterate collection in java. A running example of java program is provided that shows you how you can iterate
 
linked list
://www.roseindia.net/java/beginners/linked-list-demo.shtml...linked list  Hi i have a problem with linked list ! how and where i can use linked list? please give me some example.   Please visit
 
Facelet repeat Tag
;    This tag is used to iterate over the list of items. The name of list of items is specified by the EL expression in the value attribute...;. The literal name specified in the name attribute is used to iterate over
 
array list
array list  How to get repeate occurence values from database table in java by using arraylist
 
linked list
number of students register each semester. You are required to write a Java program to manage the registration details for the institute. 1. Use a linked list to manage the details of all registered students. a. Create your own linked list
 
DropDown list
DropDown list  how to get mysql database values into dropdown usign java servlet and ajax?   Here is a jsp code that displays the database values into dropdown list. 1)country.jsp: <%@page import="java.sql.*"%>
 
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
 
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
 
iterate through map and write the content in a separate file
iterate through map and write the content in a separate file  I am trying to save the data from file to a map, then iterate through map and write...; map: a d2rq:PropertyBridge; The last step would be to iterate through
 
Java list of uninstantiated classes
Java list of uninstantiated classes  Java list of uninstantiated classes
 
java technologies list
java technologies list  Please provide me the java technologies list
 
is list ordered in java
is list ordered in java  Hi, I want to use the list in Java... wanted to know is list ordered in Java? So, Let's know if list ordered in Java? Thanks   About the List Interface in Java The List interface
 
is list ordered in java
is list ordered in java  Hi, I want to use the list in Java... wanted to know is list ordered in Java? So, Let's know if list ordered in Java? Thanks   About the List Interface in Java The List interface
 
is list ordered in java
is list ordered in java  Hi, I want to use the list in Java... wanted to know is list ordered in Java? So, Let's know if list ordered in Java? Thanks   About the List Interface in Java The List interface
 
Java Operators List
Java Operators List  What are the Java operators, please explain and share the Java operator list.   Java Operators List Any Java beginner.../operations on two or more operands. Here is the list of operator in Java
 
Java Operators List
Java Operators List  What are the Java operators, please explain and share the Java operator list.   Java Operators List Any Java beginner.../operations on two or more operands. Here is the list of operator in Java
 
Java Operators List
Java Operators List  What are the Java operators, please explain and share the Java operator list.   Java Operators List Any Java beginner.../operations on two or more operands. Here is the list of operator in Java
 
Java Operators List
Java Operators List  What are the Java operators, please explain and share the Java operator list.   Java Operators List Any Java beginner.../operations on two or more operands. Here is the list of operator in Java
 
Java Operators List
Java Operators List  What are the Java operators, please explain and share the Java operator list.   Java Operators List Any Java beginner.../operations on two or more operands. Here is the list of operator in Java
 
Java table & list
Java table & list  I am taking string elements from user in list, And I want those list elements to be added to table row one by one, The jTable & list are at different classes but under same package. How to do so? Please
 
Ads

Related Tags for Iterate a List in Java:


Ads

 
Advertisement null

Ads