OnJava8-Examples/holding/ApplesAndOrangesWithGenerics.java

26 lines
520 B
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: holding/ApplesAndOrangesWithGenerics.java
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-05 14:05:39 -07:00
for (Apple apple : apples) {
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
*///:~