{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Generics - Stack<Student> Stack<Person> people =...

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 Document Right 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 Document Right 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 Document Right 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 Document Right 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<Student>(); Stack<Person> 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? Comparable’s compareTo() • Java 1.4 – Interface Comparable • int compareTo(Object o) • Java 1.5 – Interface Comparable<T> • int compareTo(T o) Wildcards void printArrayList(ArrayList<?> 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<? extends Shape> c) { for(Shape shape: c) { shape.draw(); } } // again, only read access, allows collections of Shape or any sub type of Shape...
View Full Document

{[ snackBarMessage ]}

Page1 / 9

Generics - Stack<Student> Stack<Person> people =...

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

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