public void testForLoop(PrintStream out) throws IOException {
List list = getList(); // initialize this list elsewhere
for (Iterator i = list.iterator(); i.hasNext(); ) {
Object listElement = i.next();
out.println(listElement.toString());
// Do something else with this list element
}
}
public void testForInLoop(PrintStream out) throws IOException {
List list = getList(); // initialize this list elsewhere
for (Object listElement : list) {
out.println(listElement.toString());
// Do something else with this list element
}
}
package com.oreilly.tiger.ch07;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ForInDemo {
public static void main(String[] args) {
// These are collections to iterate over below
List wordlist = new ArrayList();
Set wordset = new HashSet();
// Basic loop, iterating over the elements of an array
// The body of the loop is executed once for each element of args[].
// Each time through, one element is assigned to the variable word.
System.out.println("Assigning arguments to lists...");
for (String word : args) {
System.out.print(word + " ");
wordlist.add(word);
wordset.add(word);
}
System.out.println();
// Iterate through the elements of the List now
// Since lists have an order, these words should appear as above
System.out.println("Printing words from wordlist " +
"(ordered, with duplicates)...");
for (Object word : wordlist) {
System.out.print((String)word + " ");
}
System.out.println();
// Do the same for the Set. The loop looks the same but by virtue
// of using a Set, word order is lost, and duplicates are discarded.
System.out.println("Printing words from wordset " +
"(unordered, no duplicates)...");
for (Object word : wordset) {
System.out.print((String)word + " ");
}
}
}
清单 14:可以利用泛型重写清单 10
package com.oreilly.tiger.ch07;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ForInDemo {
public static void main(String[] args) {
// These are collections to iterate over below
List<String> wordlist = new ArrayList<String>();
Set<String> wordset = new HashSet<String>();
// Basic loop, iterating over the elements of an array
// The body of the loop is executed once for each element of args[].
// Each time through, one element is assigned to the variable word.
System.out.println("Assigning arguments to lists...");
for (String word : args) {
System.out.print(word + " ");
wordlist.add(word);
wordset.add(word);
}
System.out.println();
// Iterate through the elements of the List now
// Since lists have an order, these words should appear as above
System.out.println("Printing words from wordlist " +
"(ordered, with duplicates)...");
for (
String word : wordlist) {
System.out.print((String)word + " ");
}
System.out.println();
// Do the same for the Set. The loop looks the same but by virtue
// of using a Set, word order is lost, and duplicates are discarded.
System.out.println("Printing words from wordset " +
"(unordered, no duplicates)...");
for (
String word : wordset) {
System.out.print((String)word + " ");
}
}
}
public void removeListItems(PrintStream out, String[] args)
throws IOException {
List<String> wordList = new LinkedList<String>();
// Assign some words
for (int i=0; i<args.length; i++) {
wordList.add("word " + (i+1) + ": " '" + args[i] + "'");
}
// Remove all words with "1" in them.
Impossible with for/in!
for (Iterator i = wordList.iterator(); i.hasNext(); ) {
String word = (String)i.next();
if (word.indexOf("1") != -1) {
i.remove();
}
}
// You can print the words using for/in
for (String word : wordList) {
out.println(word);
}
}