OnJava8-Examples/containers/ApplesAndOrangesWithGenerics.java
Bruce Eckel bc0026c3e1 Radically reorganized
Plus many new examples in "streams" chapter.
2015-11-11 20:20:04 -08:00

27 lines
516 B
Java

// containers/ApplesAndOrangesWithGenerics.java
import java.util.*;
public class ApplesAndOrangesWithGenerics {
public static void main(String[] args) {
ArrayList<Apple> apples = new ArrayList<>();
for(int i = 0; i < 3; i++)
apples.add(new Apple());
// Compile-time error:
// apples.add(new Orange());
for(Apple apple : apples) {
System.out.println(apple.id());
}
// Using for-in:
for(Apple c : apples)
System.out.println(c.id());
}
}
/* Output:
0
1
2
0
1
2
*/