Data Structures Lakshmish Ramaswamy
Data Structures Representation of data and set of operations Different data structures permit different operations with different costs Insertion, deletion are common operations Goal – Component reuse Implement once use multiple times Example – Filing cabinet, Shelf, Drawer
Generic Protocol
Collection Interface Represents group of objects called elements of a specific type Ordered or unordered Operations boolean isEmpty() int size() boolean add(AnyType x) boolean contains(Object x) boolean remove(Object x) Void clear() Object[] toArray() Java.util.Iterator<AnyType> iterator()
Core Collection Interfaces True collections Can be manipulated as collections (have collection-view operations) Java Collections Framework
Iterator Pattern Consider a collection of objects stored in an array How to print the array elements for(int i = 0; i < (n-1); i++) System.out.println(v[i]); i controls the iteration i ranges from 0 to (n-1) – all elements in the array i is an iterator
Iterator Design Iterator class encapsulates position inside of a collection Provides methods to step through elements for(itr = v.first(); itr.isValid(); itr.advance()) System.out.println(itr.getData())
Generic Iterator Interface
Sample Iterator
Iterator Example
Implementation of Iterator Interface
Printing Elements of Collection
interface List extends Collection Lists are collections where the elements have an order Each element has a definite position (first, second, third, …) Positions are generally numbered from 0 Duplicates may be allowed methods of List: Object get(int pos) – return element at position pos boolean set(int pos, Object elem) – store elem at position pos boolean add(int pos, Object elem) – store elem at position pos; slide elements at position pos to size( )-1 up one position to the right Object remove(int pos) – remove item at given position; shift remaining elements to the left to fill the gap; return the removed element int indexOf(Object o) – return position of first occurrence of o in the list, or -1 if not found Java Collections Framework
ListIterator The iterator( ) method for a List returns an instance of ListIterator Can also send listIterator(int pos) to get a ListIterator starting at the given position in the list ListIterator returns objects in the list collection in the order they appear in the collection Supports additional methods: hasPrevious( ), previous( ) – for iterating backwards through a list set(Object o) – to replace the current element with something else add(Object o) – to insert an element after the current element Java Collections Framework
Some examples: a.set(i,a.get(j).times(a.get(k)) What is an equivalence of the following expression: a[i] = a[j].times(a[k]) for List? a.set(i,a.get(j).times(a.get(k)) Write a method to swap two indexed values in a List. private static void swap(List a, int i, int j) { Object tmp = a.get(i); a.set(i, a.get(j)); a.set(j, tmp); } Java Collections Framework
Some examples: Write a method to randomly shuffle a list (using swap) public static void shuffle(List list, Random rnd) { for (int i=list.size(); i>1; i--) swap(list, i-1, rnd.nextInt(i)); } Java Collections Framework