Advertisement
m1o2

Rabe3, Generics, Java, ArrayList

Dec 6th, 2012
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. package generics;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class Generics_Main {
  6.  
  7.     public static void main(String[] args) {
  8.    
  9.         // Base is abstract
  10.         // Derivative1 extends Base
  11.         // Derivative2 extends Base
  12.  
  13.         ArrayList<Derivative1> derivative1s = new ArrayList<>();
  14.         ArrayList<Derivative2> derivative2s = new ArrayList<>();
  15.  
  16.         ArrayList<String> strings = new ArrayList<>();
  17.  
  18.         foo( derivative1s );
  19.         foo( derivative2s );
  20.  
  21.         foo( strings ); // not working. if u want it to work, replace the
  22.                         // "<K extends Base>" with "<K>"
  23.     }
  24.  
  25.     public static <K extends Base> void foo(ArrayList<K> list) {
  26.  
  27.         // do something
  28.         for (K k : list) {
  29.             // do something
  30.         }
  31.         // do something
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement