24 lines
592 B
Java
24 lines
592 B
Java
// collections/ApplesAndOrangesWithGenerics.java
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com 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
|
|
*/
|