![Page 1: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/1.jpg)
Lists
Discussion F
![Page 2: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/2.jpg)
Generics Tutorial(Ref: Gilad Baracha)
● Auto Boxing
● Erasure
● Sub Classes
● Unchecked Type cast
● Bounded Wildcards
![Page 3: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/3.jpg)
Iterator
List<Integer> lint = new ArrayList<Integer>(); //1lint.add(new Integer(100)); //2
Iterator<Integer> intIter = lint.iterator(); //3 Integer i1 = intIter.next(); //4
lint.add(101); //5int i2 = intIter.next(); //6ConcurrentModificationException
![Page 4: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/4.jpg)
Erasure
public class X { public X();} //7public class Y extends X { public Y();} //8
List<X> lx = new ArrayList<X>(); //9List<Y> ly = new ArrayList<Y>(); //10
System.out.println(lx.getClass()==ly.getClass());//12
true
![Page 5: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/5.jpg)
Erasure (2)
public class X { public X();} //7public class Y extends X { public Y();} //8
List<X> lx = new ArrayList<X>(); //9List<Y> ly = new ArrayList<Y>(); //10
if (lx instanceof ArrayList<X>) {}//unchecked warning
List<X> otherlx = (List<X>) lx;
![Page 6: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/6.jpg)
Subtype
public class X { public X();} //7public class Y extends X { public Y();} //8
List<X> lx = new ArrayList<X>(); //9List<Y> ly = new ArrayList<Y>(); //10lx = ly; //11Compiler Error
![Page 7: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/7.jpg)
Subtype (2)
void printCollection(Collection c) { //13 Iterator i = c.iterator(); while(i.hasNext()) { System.out.println(i.next()); } }
void printCollection(Collection<Object> c) { //14 for (Object o: c) { System.out.println(o); } }
printCollection(lint); //??
![Page 8: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/8.jpg)
Bounded Wildcards
● Used to express polymorphism
● Wildcard, means some type– <?>
● lower bound, some super type of T– <? super T>
● upper bound, some sub class of B– <? extends B>
![Page 9: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/9.jpg)
Wildcards void printCollection(Collection<?> c){ for (Object o: c) { System.out.println(o); } }
Collection<?> c = new ArrayList<Integer>(); c.add(new Object()); // Error
void printCollection(Collection<? extends Shape> c){ for (Shape s: c) { System.out.println(s.area()); } }
![Page 10: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/10.jpg)
Generic Functions <T> void arrayToCollection(T[] a, Collection<T> c) { for (T t: a) { c.add(t); } } Integer [] a = new Integer[10];... Collection<Integer> c = new ArrayList<Integer>(); arrayToCollection(a, c);
•No need to pass actual type, compiler infers it •They can have bounds too •Used when arguments/return type are correlated
![Page 11: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/11.jpg)
Inner Classes
● Block Scoped– definition similar to definition of a field or a
method
● Static– do not have the block scope
![Page 12: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/12.jpg)
Snippets // 1
public class X { //1 private int fx;
public class I { int fi; public I() { fi = fx; } } }
X.I ci = new X.I(); java X$I // to run main method in X.I
![Page 13: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/13.jpg)
// 2
public class X { //2 private int fx; private I i = new I(); private int fx2 = i.fi;
public class I { private int fi; public I() { fi = fx; } } }
![Page 14: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/14.jpg)
// 3
public class X { //3 int fx;
public static class I { int fi; public I() { fi = fx; } }}
![Page 15: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/15.jpg)
DisInherit Methods
● Not really, has to satisfy Is-a
● Illegal to narrow visibility
● Can reimplement and throw exception
![Page 16: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/16.jpg)
DisInheritpublic class X { public void function() {}}
public class Y extends X { private void function() {} // compiler error}
public class Z extends X { private void function() { throw new NoSuchMethodException(); }}
![Page 17: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/17.jpg)
Lisp List
car cdr car cdr
![Page 18: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/18.jpg)
Lisp List Interface
public interface LispList<E> {
public int length(); public E first (); public LispList<E> rest ();
}
![Page 19: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/19.jpg)
Lisp Listpublic Cons<E> implements LispList<E> {
E car; LispList<E> cdr;
public Cons(E e) {car = e;} public Cons(LispList<E> onelist, LispList<E> otherlist) {}
public int length() {} public E first () {return car;} public LispList<E> rest () {return cdr;}
}
![Page 20: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/20.jpg)
Lisp List
● Empty list● How to construct from two lists?● How to find length?
![Page 21: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/21.jpg)
List of Arrays
100 100 100
1 2 3
![Page 22: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/22.jpg)
List Of Arrays
● Each node has same capacity– can compute how many nodes from start the
required index will be
● nodes have different capacity– can balance the list length
![Page 23: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/23.jpg)
Using List<E>
public class ListOfArrays<E> {
int totalCount; int totalCapacity; int nodeCapacity; List<E []> list;
private void checkIndex(int idx) {} public E get(int idx){} public void set(int idx, E e) {}
}
![Page 24: Lists Discussion F. Generics Tutorial (Ref: Gilad Baracha) ● Auto Boxing ● Erasure ● Sub Classes ● Unchecked Type cast ● Bounded Wildcards](https://reader030.vdocuments.net/reader030/viewer/2022032801/56649d535503460f94a2ee38/html5/thumbnails/24.jpg)
List of Arrayspublic class ListOfArrays<E> {
int totalCount; int totalCapacity; List<DataNode<E>> list;
private class DataNode<E> { int count; int capacity; E [] array; } private void checkIndex(int idx) {} public E get(int idx){} public void set(int idx, E e) {}}