Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supporting code for the 'There's a loop for each of us' blog post
package com.oopuniversity.basics.loops;
import com.oopuniversity.basics.classes.Person;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by OOPUniversity on 11/15/2015.
*/
public class ForEach {
public static void main(String[] args) {
List personList = buildPersonList();
String [] simpleList = { "First", "Second", "Fifteenth?" };
printPersonList1(personList);
printPersonList2(personList);
printPersonList3(personList);
printPersonList4(personList);
printPersonList5(personList);
printStringArray(simpleList);
}
private static List <Person> buildPersonList() {
List <Person> personList = new ArrayList<>();
Person p = new Person("John", "Doe");
personList.add(p);
p = new Person("Satoshi", "Nakamoto");
personList.add(p);
p = new Person("Athena", "Momandpopadopolis");
personList.add(p);
return personList;
}
public static void printPersonList1(List personList) {
System.out.println("Using a 'while' loop the JDK 1.4 (and earlier) way:");
Iterator iter = personList.iterator();
while(iter.hasNext()) {
Person p = (Person) iter.next();
System.out.println(p.getFirstName() + " " + p.getLastName());
}
System.out.println();
}
public static void printPersonList2(List personList) {
System.out.println("Using a 'while' loop with generics:");
Iterator <Person> iter = personList.iterator();
while(iter.hasNext()) {
Person p = iter.next();
System.out.println(p.getFirstName() + " " + p.getLastName());
}
System.out.println();
}
public static void printPersonList3(List<Person> personList) {
System.out.println("Using a 'for' loop with generics:");
Person p = null;
for(Iterator <Person> iter = personList.iterator(); iter.hasNext(); ) {
p = iter.next();
System.out.println(p.getFirstName() + " " + p.getLastName());
}
System.out.println();
}
public static void printPersonList4(List<Person> personList) {
System.out.println("Using a 'for' loop with no iterator:");
Person p = null;
for(int i = 0; i < personList.size(); i++) {
p = personList.get(i);
System.out.println(p.getFirstName() + " " + p.getLastName());
}
System.out.println();
}
public static void printPersonList5(List<Person> personList) {
System.out.println("Using a 'for each' loop:");
for(Person p:personList) {
System.out.println(p.getFirstName() + " " + p.getLastName());
}
System.out.println();
}
private static void printStringArray(String[] aList) {
System.out.println("The enhanced for can also work on array types:");
for(String s: aList) {
System.out.println(s);
}
System.out.println();
}
}
package com.oopuniversity.basics.classes;
/**
* Created by OOPUniversity on 11/15/2015.
*/
public class Person {
private String firstName;
private String lastName;
public Person(String newFirstName, String newLastName) {
firstName = newFirstName;
lastName = newLastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment