2015-04-20 15:36:01 -07:00
|
|
|
|
//: holding/ApplesAndOrangesWithGenerics.java
|
2015-05-29 14:18:51 -07:00
|
|
|
|
// <20>2015 MindView LLC: see Copyright.txt
|
2015-04-20 15:36:01 -07:00
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
public class ApplesAndOrangesWithGenerics {
|
|
|
|
|
public static void main(String[] args) {
|
2015-05-05 11:20:13 -07:00
|
|
|
|
ArrayList<Apple> apples = new ArrayList<>();
|
2015-04-20 15:36:01 -07:00
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
|
|
|
apples.add(new Apple());
|
|
|
|
|
// Compile-time error:
|
|
|
|
|
// apples.add(new Orange());
|
2015-05-18 23:05:20 -07:00
|
|
|
|
for(Apple apple : apples) {
|
2015-05-05 14:05:39 -07:00
|
|
|
|
System.out.println(apple.id());
|
|
|
|
|
}
|
2015-04-20 15:36:01 -07:00
|
|
|
|
// Using foreach:
|
|
|
|
|
for(Apple c : apples)
|
|
|
|
|
System.out.println(c.id());
|
|
|
|
|
}
|
|
|
|
|
} /* Output:
|
|
|
|
|
0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
0
|
|
|
|
|
1
|
|
|
|
|
2
|
|
|
|
|
*///:~
|