GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — JavaLearningPath/Generics/src/TypeInference.java at master · AlexGithubbb/JavaLearningPath Expand file tree/ Copy pathTypeInference.javaMore file actions 66 lines (51 loc) · 1.74 KB / Copy pathTypeInference.javaFile metadata and controls66 lines (51 loc) · 1.74 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 public class TypeInference { public static class BoxDemo { public static class Box<T>{ private T t; public void set( T t){ this.t = t; } public T get(){ return t; } } public static <T> void addBox(T t, java.util.List<Box<T>> boxes){ Box<T> box = new Box<>(); box.set(t); boxes.add(box); } public static <U> void outputBoxes(java.util.List<Box<U>> boxes) { int counter = 0; for (Box<U> box: boxes) { U boxContents = box.get(); System.out.println("Box #" + counter + " contains [" + boxContents.toString() + "]"); counter++; } } public static void main(String[] args) { java.util.ArrayList<Box<Integer>> listOfIntegerBoxes = new java.util.ArrayList<>(); BoxDemo.<Integer>addBox(10, listOfIntegerBoxes); BoxDemo.addBox(20, listOfIntegerBoxes); BoxDemo.addBox(30, listOfIntegerBoxes); BoxDemo.outputBoxes(listOfIntegerBoxes); } } /*Type Inference and Generic Constructors of Generic and Non-Generic Classes*/ class MyClass<X> { <T> MyClass(T t) { } } MyClass<Integer> myObject = new MyClass<>("hello"); /* static <T> List<T> emptyList(); List<T> list = Collections.emptyList(); void processStringList(List<String> stringList) { // process stringList } processStringList(Collections.emptyList()); // not work in JAVA SE 7 // has to be like this in SE 7: processStringList(Collections.<String>emptyList()); */ } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |