L7_print

L7_print - CSE 12: Basic data structures and...

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

View Full Document Right Arrow Icon
: Basic data structures and object-oriented design Jacob Whitehill jake@mplab.ucsd.edu Lecture Seven 10 Aug 2011 Wednesday, August 10, 2011 More on Java generics. Wednesday, August 10, 2011 Making ArrayList generic We’ve already discussed the bene f t 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 Making List generic Before creating a generic ArrayList class, let’s go back to the “contract” between implementor and user -- the List interface. The bene f ts of a generic interface are exactly analogous to the bene f ts 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 As described before, when writing a generic List , we include a type parameter at the start of the class de f nition. The type parameter tells the generic List interface which type of element the list can accept. We can de f ne 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 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 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 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); } 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 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

L7_print - CSE 12: Basic data structures and...

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

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