Home Techindex Iterate a List in Java

 
 

Iterate a List in Java
Posted on: September 28, 2010 at 12:00 AM
Advertisement
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... data into it as // Declaring ArrayList object of type String List myList... elements to the list myList.add("Rohan"); myList.add("Johan"); myList.add
 
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
 
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
 
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
 
Java list
Java list  What is the difference between a Choice and a List
 
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
 
Linked list
Linked list  what is difference btw linked list in datastructure and linked list in java
 
Java list of uninstantiated classes
Java list of uninstantiated classes  Java list of uninstantiated classes
 
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
 
java technologies list
java technologies list  Please provide me the java technologies list
 
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

 
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
 
array list
array list  How to get repeate occurence values from database table in java by using arraylist
 
Iterate Tag used with a Map.
Iterate Tag used with a Map.  How Iterate Tag used with a Map
 
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.*"%>
 
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
 
list - Java Interview Questions
list  Hi all Naturally in java a list will allow duplicates, but if i want the list which shouldn't allow duplicates then what should be the logic...) method of Interface List public boolean contains(Object o) Returns
 
java linked list urgent!!!
java linked list urgent!!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
java linked list urgent !!!
java linked list urgent !!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
java linked list urgent !!!
java linked list urgent !!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
java linked list urgent !!!
java linked list urgent !!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
java linked list urgent !!!
java linked list urgent !!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
java linked list urgent !!!
java linked list urgent !!!  Q1. Generate 10 thousand random integers with values in the range between 1 to 100. Q2. Store each randomly generated number into a node and then attach the node to a linked list. The list must
 
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
 
spilt list - Java Beginners
list containing an even number of nodes, say 2k, into two circular lists each... new lists linked to by parameters sub1, and sub2 from the list initially referenced by parameter list. After ListSplit has executed, the first parameter should
 
Core java linked list example
Core java linked list example  What is the real time example for linked list
 
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
 
Iterate java collection

 
java list program for common elements
java list program for common elements  hi I want program using java collections list which takes two array list elements and display common elements in both the list as an output   Please visit the following link
 
linked list in java - Java Beginners
linked list in java  Hi, how to implement linked list in java using... information. http://www.roseindia.net/java/beginners/linked-list-demo.shtml...; static List values; static { Integer vals[] = new Integer[N]; Random rn = new
 
list of predeined methods and use java
list of predeined methods and use java  I need list of predefined methods in java like reverse,compare,tostring, etc
 
What is the List interface?
What is the List interface?  Hi, What is the List interface? thanks,   The list interface is nothing but a subtype of tha java ordered... is list Interface in Java Program
 
Broken link list
Broken link list   Please provide me a java code to find the broken link list of url's for a given url
 
Order of list in java Vs Haskell.
Order of list in java Vs Haskell.  How to order of [1,2,3] to [1,2,3,2,1]in java
 
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
 
Link List proble, - Java Beginners
/java/beginners/linked-list-demo.shtml Hope that it will be helpful for you...Link List proble,  i have a problem about programming... this is the problem.. -create an application using Linked List this application can add
 
Linked list implementation
Linked list implementation  How to create linkedlist by using array in java? and also How to manipulate
 
link list in c++
link list in c++  What is hash table in java,what is use? and how to use
 
HELP Generic linked list
HELP Generic linked list  How to create Generic linked list example program in Java
 
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
 
Scrolling List Box. - Java Beginners
Scrolling List Box.  How can is make a list box scrollable by using method ? Please give me the code snipetts
 
Converting java arrays into list
. asList() It converts the object array into the fixed sized list Example...[]={111,222,333,444};         List list=new ArrayList();         List list1=new ArrayList();         list=Arrays.asList(ar);         list1=Arrays.asList(ar1
 
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
 
Advertisement

Related Tags for Iterate a List in Java:


Advertisements Advertisements
 

 

 

DMCA.com