Main Tutorials

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 Author

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

Comments

Subscribe
Notify of
28 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
gaple qiu qiu
6 years ago

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 alternatif
6 years ago
Reply to  gaple qiu qiu

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

sbobet mobile
6 years ago
Reply to  gaple qiu qiu

your help is very usefull.. i was appreciate it

BandarQ
6 years ago

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

aladincash7
6 years ago

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

aladincash7
6 years ago

nice artikel

bola bet
6 years ago

nice artikel

GanoolQQ
6 years ago

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

andriano
6 years ago

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
6 years ago

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

sbobet asia
6 years ago

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);
}

sbobet asia
6 years ago

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
6 years ago

Thanks for list implementaion trying one for my ibatis project.

sabung ayam pw
6 years ago

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
8 years ago

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
9 years ago

which one is more efficient

Vrutin
9 years ago

Thanks Mkyong

Himansu Nayak
9 years ago

we can also use do while loop also

Java man
10 years ago

mkyong you are da shiiiiiiiiiiiiiiiittttttt!!

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

Chaithanya Bangaru
10 years ago

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
9 years ago

thats why the enhanced loop is made

Krishnamoorthy
11 years ago

Thanks man.. This helped a lot

Mark
11 years ago

@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
11 years ago

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

Amar
11 years ago

i wanto display ABC-123 in CBA-321 format

Rajneesh
13 years ago

Thanks for list implementaion trying one for my ibatis project.

r30
14 years ago

4. For-each loop (java5 feature):

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

anurag
6 years ago

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