L7 - CSE 12: Basic data structures and object-oriented...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 12 : Basic data structures and object-oriented design Jacob Whitehill jake@mplab.ucsd.edu Lecture Seven 10 Aug 2011 Wednesday, August 10, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
More on Java generics. Wednesday, August 10, 2011
Background image of page 2
Making ArrayList generic We’ve already discussed the beneft to the user oF making a collection generic. We also saw (in brieF) how to instantiate a generic type, and a bit on how to implement a generic data structure. But let’s be a bit more precise. We’ll keep going with the ArrayList<T> example. Wednesday, August 10, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Making List generic Before creating a generic ArrayList class, let’s go back to the “contract” between implementor and user -- the List interface. The beneFts of a generic interface are exactly analogous to the beneFts of a generic class: When you use a variable of the interface type, the compiler will check that the types are consistent: final List<Integer> list = . .. // some concrete impl list.add(new Integer(5)); // ok list.add(“test”); // not ok -- compile-time error Wednesday, August 10, 2011
Background image of page 4
As described before, when writing a generic List , we include a type parameter at the start of the class deFnition. The type parameter tells the generic List interface which type of element the list can accept. We can deFne a generic List interface as follows: interface List <T> { int size (); void add (T element); T get (int index); void remove (int index); } Making List generic Wednesday, August 10, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Generics syntax Let’s examine more carefully how the syntax works: interface List<T> { int size (); void add (T element); T get (int index); void remove (int index); } Wednesday, August 10, 2011
Background image of page 6
Generics syntax Let’s examine more carefully how the syntax works: interface List <T> { int size (); void add (T element); T get (int index); void remove (int index); } When we write angled brackets just after the type name, we are declaring a type parameter . Here, the type parameter name is T . void method (Student student ) { student.setAge(24); student.printAddress(); } This is analogous, in a Java method signature, to declaring a data parameter and giving it the name student . Wednesday, August 10, 2011
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Let’s examine more carefully how the syntax works: interface List<T> { int size (); void add ( T element); T get (int index); void remove (int index); } Following the declaration of type parameter T, whenever we write T, we are using the type parameter’s value . void method (Student student) { student .setAge(24); student .printAddress(); } This is analogous, in a Java method signature, to using that parameter inside the method body. Wednesday, August 10, 2011
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 31

L7 - CSE 12: Basic data structures and object-oriented...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online