25-list - CSE 143 Lecture 25 Advanced collection classes...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 25 Advanced collection classes (ADTs; interfaces; abstract classes; inner classes; generics; iterators; hashing) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Our list classes • We implemented the following two list classes: – ArrayIntList – LinkedIntList – Problems: We should be able to treat them the same way in client code. • Some of their methods are implemented the same way (redundancy). • Linked list carries around a clunky extra node class. • They can store only int elements, not any type of value. • It is inefficient to get or remove each element of a linked list. 17 -3 42 value 2 1 0 index front 42 next data -3 next data 17 next data
Background image of page 2
3 Recall: ADT interfaces (11.1) abstract data type (ADT) : A specification of a collection of data and the operations that can be performed on it. – Describes what a collection does, not how it does it. • Java's collection framework describes ADTs with interfaces: – Collection , Deque , List , Map , Queue , Set , SortedMap • An ADT can be implemented in multiple ways by classes: – ArrayList and LinkedList implement List – HashSet and TreeSet implement Set – LinkedList , ArrayDeque , etc. implement Queue • Exercise: Create an ADT interface for the two list classes.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 An IntList interface (16.4) // Represents a list of integers. public interface IntList { public void add(int value); public void add(int index, int value); public int get(int index); public int indexOf(int value); public boolean isEmpty(); public void remove(int index); public void set(int index, int value); public int size(); } public class ArrayIntList implements IntList { ... public class LinkedIntList implements IntList { ...
Background image of page 4
5 Our list classes • We have implemented the following two list collection classes: – ArrayIntList – LinkedIntList – Problems: • We should be able to treat them the same way in client code. Some methods are implemented the same way (redundancy). • Linked list carries around a clunky extra node class. • They can store only int elements, not any type of value. • It is inefficient to get or remove each element of a linked list. 17 -3 42 value 2 1 0 index front 42 next data -3 next data 17 next data
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Common code • Notice that some of the methods are implemented the same way in both the array and linked list classes. – add( value ) – contains – isEmpty • Should we change our interface to a class? Why / why not? – How can we capture this common behavior?
Background image of page 6
7 Abstract classes (9.6) abstract class : A hybrid between an interface and a class. – defines a superclass type that can contain method declarations (like an interface) and/or method bodies (like a class) – like interfaces, abstract classes that cannot be instantiated (cannot use new to create any objects of their type) • What goes in an abstract class? – implementation of common state and behavior that will be
Background image of page 7

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

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

Page1 / 43

25-list - CSE 143 Lecture 25 Advanced collection classes...

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

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