26-advanced_collections

26-advanced_collections - CSE143...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators)  read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding 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 Document Right 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. They can store only  int  elements, not any type of value. Some of their methods are implemented the same way (redundancy). Linked list carries around a clunky extra node class. It is inefficient to get or remove each element of a linked list. inde x 0 1 2 valu e 4 2 -3 1 7 front data next 42 data next -3 data next 17
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 Document Right 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 boolean contains(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. They can store only  int  elements, not any type of value. Some methods are implemented the same way (redundancy). Linked list carries around a clunky extra node class. It is inefficient to get or remove each element of a linked list. inde x 0 1 2 valu e 4 2 -3 1 7 front data next 42 data next -3 data next 17
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
6 Type Parameters (Generics) ArrayList< Type > name = new ArrayList< Type >(); Recall: When constructing a  java.util.ArrayList , you  specify the type of elements it will contain between  <  and  > . We say that the 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 30

26-advanced_collections - CSE143...

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

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