How to loop / iterate a List in Java

Here i show you four ways to loop a List in Java.

  1. Iterator loop
  2. For loop
  3. For loop (Adcance)
  4. While loop

package com.mkyong.core;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ArrayToList {
     public static void main(String[] argv) {

	String sArray[] = new String[] { "Array 1", "Array 2", "Array 3" };

	// convert array to list
	List<String> lList = Arrays.asList(sArray);

	// iterator loop
	System.out.println("#1 iterator");
	Iterator<String> iterator = lList.iterator();
	while (iterator.hasNext()) {
		System.out.println(iterator.next());
	}

	// for loop
	System.out.println("#2 for");
	for (int i = 0; i < lList.size(); i++) {
		System.out.println(lList.get(i));
	}

	// for loop advance
	System.out.println("#3 for advance");
	for (String temp : lList) {
		System.out.println(temp);
	}

	// while loop
	System.out.println("#4 while");
	int j = 0;
	while (j < lList.size()) {
		System.out.println(lList.get(j));
		j++;
	}
    }
}

Output


#1 iterator
Array 1
Array 2
Array 3
#2 for
Array 1
Array 2
Array 3
#3 for advance
Array 1
Array 2
Array 3
#4 while
Array 1
Array 2
Array 3

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
gaple qiu qiu
Guest
gaple qiu qiu

for (int i = 0; i < lList.size(); i++) {
The above line can be replaced with

int listSize = lList.size();
for (int i = 0; i < listSize; i++) {

Thus avoiding the size method check in every iteration..this achieves great performance if you have a large list..

sbobet mobile
Guest
sbobet mobile

your help is very usefull.. i was appreciate it

sbobet alternatif
Guest
sbobet alternatif

int listSize = lList.size();
for (int i = 0; i < listSize; i++) {
this is the code i needed

aladincash7
Guest
aladincash7

Artikelnya bagus, Informasi yang diberikan juga lengkap dan mudah di baca…
sukses terus ya gan :))

aladincash7
Guest
aladincash7

nice artikel

bola bet
Guest
bola bet

nice artikel

BandarQ
Guest
BandarQ

Artikelnya bagus, Informasi yang diberikan juga lengkap dan mudah di baca..

GanoolQQ
Guest
GanoolQQ

for (int i = 0; i < lList.size(); i++) {
The above line can be replaced with

andriano
Guest
andriano

for (int i = 0; i < lList.size(); i++) {
The above line can be replaced with

int listSize = lList.size();
for (int i = 0; i < listSize; i++) {

Thus avoiding the size method check in every iteration..this achieves great performance if you have a large list

stevanie
Guest
stevanie

Thus avoiding the size method check in every iteration ,so usefull this theory ,i will try it

sbobet asia
Guest
sbobet asia

He showed that one already as his #3 loop “for loop advance”

// for loop advance
System.out.println(“#3 for advance”);
for (String temp : lList) {
System.out.println(temp);
}

anurag
Guest
anurag

nice, awesome, and very clean and well explained java example
thanks keep posting.

sbobet asia
Guest
sbobet asia

String sArray[] = new String[] { “Array 1”, “Array 2”, “Array 3” };

final List listPage = Arrays.asList(sArray);

int numPage = 0;

while (numPage< listPage.size()) {

numPage++;
}

Hanson P
Guest
Hanson P

Thanks for list implementaion trying one for my ibatis project.

sabung ayam pw
Guest
sabung ayam pw

He showed that one already as his #3 loop “for loop advance”

// for loop advance
System.out.println(“#3 for advance”);
for (String temp : lList) {
System.out.println(temp);
}

Robby Ali Darmawan
Guest
Robby Ali Darmawan

String sArray[] = new String[] { “Array 1”, “Array 2”, “Array 3” };

final List listPage = Arrays.asList(sArray);

int numPage = 0;

while (numPage< listPage.size()) {

numPage++;
}

can I Convert String [ ] to int like this?

edwin
Guest
edwin

which one is more efficient

Vrutin
Guest
Vrutin

Thanks Mkyong

Himansu Nayak
Guest
Himansu Nayak

we can also use do while loop also

Java man
Guest
Java man

mkyong you are da shiiiiiiiiiiiiiiiittttttt!!

You always appear when I am in need of some serious Java help :D

Chaithanya Bangaru
Guest
Chaithanya Bangaru

for (int i = 0; i < lList.size(); i++) {
The above line can be replaced with

int listSize = lList.size();
for (int i = 0; i < listSize; i++) {

Thus avoiding the size method check in every iteration..this achieves great performance if you have a large list..

Himansu Nayak
Guest
Himansu Nayak

thats why the enhanced loop is made

Krishnamoorthy
Guest
Krishnamoorthy

Thanks man.. This helped a lot

Mark
Guest
Mark

@r30:

He showed that one already as his #3 loop “for loop advance”

// for loop advance
System.out.println(“#3 for advance”);
for (String temp : lList) {
System.out.println(temp);
}

raj
Guest
raj

i wanto display string ABC-123 in CBA-321 format

Amar
Guest
Amar

i wanto display ABC-123 in CBA-321 format

Rajneesh
Guest
Rajneesh

Thanks for list implementaion trying one for my ibatis project.

r30
Guest
r30

4. For-each loop (java5 feature):

//for-each loop
for (String element : lList) // or sArray
{
System.out.println( element );
}

seo paketleri
Guest
seo paketleri

thank you so much. really.