Generics - Stack<Student>();...

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

View Full Document Right Arrow Icon
Generic types for ArrayList Old Java (1.4 and older): ArrayList strings = new ArrayList(); strings.enqueue(“hello”); String word = (String) strings.get(0); New (since 1.5): ArrayList<String> strings = new ArrayList <String>(); strings.add(“hello”); String word = strings.get(0);
Background image of page 1

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

View Full DocumentRight Arrow Icon
Advantages Better readability Better type-safety: no casts (runtime checks), compiler can catch problems
Background image of page 2
Writing your own generic code Formal type parameter public class Stack<E> { … } convention: Short (single-char) uppercase can be used wherever a Type is needed will be replaced with actual Type
Background image of page 3

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

View Full DocumentRight Arrow Icon
Writing your own generic code public class Stack<E> { public void push(E element) { contents.add(element); } public E pop() { int top = contents.size()-1; E result = contents.get(top); contents.remove(top); return result; } private ArrayList<E> contents = new ArrayList<E>(); }
Background image of page 4
Problems with sub-types class Student extends Person { . . } Stack<Student> students = new
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Stack&lt;Student&gt;(); Stack&lt;Person&gt; people = students; // should this be possible? No, because Person sam = new Person(); people.push(sam); // if this was legal, we would have just sneaked a non-student onto the students list on the other hand, how do you write generic code to accept stacks of sub-types of Person? Comparables compareTo() Java 1.4 Interface Comparable int compareTo(Object o) Java 1.5 Interface Comparable&lt;T&gt; int compareTo(T o) Wildcards void printArrayList(ArrayList&lt;?&gt; al) { for(Object o: al) { System.out.println(o); } } // only read access (everything is an Object), but cannot add, because do not know correct type (except null, which is any type) Bounded wildcards public void drawAll(Collection&lt;? extends Shape&gt; c) { for(Shape shape: c) { shape.draw(); } } // again, only read access, allows collections of Shape or any sub type of Shape...
View Full Document

This note was uploaded on 05/06/2010 for the course COMPUTER S 101 taught by Professor Sanaodeh during the Spring '08 term at NYU.

Page1 / 9

Generics - Stack&amp;amp;lt;Student&amp;amp;gt;();...

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