OnJava8-Examples/collections/ApplesAndOrangesWithGenerics.java
2016-01-25 18:05:55 -08:00

24 lines
610 B
Java

// collections/ApplesAndOrangesWithGenerics.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
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());
}
}
}
/* Output:
0
1
2
*/