JAVA-MANI.BLOGSPOT.COM
Tuesday, August 12, 2008
Using for..each like command to iterate arrays or a list can be much simplified our code. Below is an example how to do it in Java, the first loop is for iterating array and the second for iterating a list containing a some names.


package org.kodejava.example.lang;

import java.util.ArrayList;
import java.util.List;

public class ForEachExample {
public static void main(String[] args) {
Integer[] numbers = {10, 100, 1000, 10000, 100000, 1000000};

for (Integer i : numbers) {
System.out.println("Number: " + i);
}

List names = new ArrayList();
names.add("James");
names.add("Joshua");
names.add("Scott");

for (String name : names) {
System.out.println("Name: " + name);
}
}
}

0 comments:

SUBSCRIBE VIA eMAIL

Enter your email address:

Delivered by FeedBurner

Recent Posts

Firefox 3

Counter

internet companies

Live Traffic Map

Subscribe Now