Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.

Similar presentations


Presentation on theme: "CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18."— Presentation transcript:

1 CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18

2 Let’s build a BagAST using an array of Objects. Arrays are simple to use but also have a fixed size.

3 public class ArrayBag implements BagADT { /* Local data to implement a Bag */ /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

4 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

5 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { itemCount = 0; INIT_SIZE = 100; items = new Object[INIT_SIZE]; } /* Implementations for add, remove, isEmpty and clone */ }

6 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { return (itemCount == 0); } }

7 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) { if (item == null) throw new NullPointerException(); if (itemCount >= INIT_SIZE) throw new Error(); items[itemCount] = item; itemCount++; }}

8 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException { if (itemCount == 0) throw new NoSuchElementException(); else { itemCount--; return items[itemCount] ; } }

9 public class ArrayBag implements BagADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException {…} public ArrayBag clone() { ArrayBag copy = new ArrayBag(); copy.itemCount = itemCount; copy.items = items.clone(); return copy; } }

10 Examples of using ArrayBag: ArrayBag bag = new ArrayBag(); bag.add(1); bag.add(2); bag.add(3); bag.add(1); bag.add(2); bag.add(3); printBag(bag); printBag(bag); int item = (int) bag.remove(); int item = (int) bag.remove(); System.out.println(item); System.out.println(item); Output is:3213

11 Using the Object class in BagADT can be problematic You have to type-cast all objects returned by remove () (Why?) It is hard to enforce a uniform type in a bag. Bag declarations are uninformative. (All bags are essentially the same)

12 Java Generics Generics allow you to add a type parameter to an interface or class: BagADT or ArrayBag

13 When a type is declared, a class name replaces the type parameter: ArrayBag or ArrayBag Only the declared type can be inserted. Removed items need not be type-cast.

14 public interface BagADT { void add(E item); E remove () throws NoSuchElementException ; boolean isEmpty(); BagADT clone(); }

15 public class ArrayBag implements BagADT { /* Local data to implement a Bag */ /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

16 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; /* One or more constructors */ /* Implementations for add, remove, isEmpty and clone */ }

17 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { itemCount = 0; INIT_SIZE = 100; // Kludge alert! items = (E[]) new Object[INIT_SIZE]; } /* Implementations for add, remove, isEmpty and clone */ }

18 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { return (itemCount == 0); } }

19 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(E item) { if (item == null) throw new NullPointerException(); if (itemCount >= INIT_SIZE) throw new Error(); items[itemCount] = item; itemCount++; }}

20 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public E remove() throws NoSuchElementException { if (itemCount == 0) throw new NoSuchElementException(); else { itemCount--; return items[itemCount] ; } }

21 public class ArrayBag implements BagADT { private E[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException {…} public ArrayBag clone() { ArrayBag copy = new ArrayBag (); copy.itemCount = itemCount; copy.items = items.clone(); return copy; } }

22 printBag becomes: public void printBag(BagADT myBag){ BagADT temp = myBag.clone(); while(! temp.isEmpty()) { System.out.println(temp.remove()); } }

23 Examples of using ArrayBag in Generic form: ArrayBag bag = new ArrayBag (); bag.add(1); bag.add(2); bag.add(33); bag.printBag(bag); int item = bag.remove(); // No casting! System.out.println(item); Output is:332133

24 List ADT A List is an ordered collection of items. Each item has a position, starting at 0. Item: “a” “b” “c” “d” “e” Position: 0 1 2 3 4

25 Like an array, a list can be indexed. But, a list can grow or shrink in size. A size of zero (an empty list) is allowed.

26 Operations in a ListADT void add(E item) Add where? At right end of list. void add(int pos, E item) add add does not overwrite items, so list size grows. pos Valid values for pos are 0 <= pos <= size()-1

27 boolean contains(E item) E Is E already in the list? equals(item) Use equals(item) to test membership. int size() Zero size is OK. boolean isEmpty() size() == 0 Same as size() == 0

28 E get(int pos) pos Return value at pos. Non-destructive. 0 <= pos <= size()-1 Requires 0 <= pos <= size()-1 E remove(int pos) pos Remove and return value at pos. Is destructive. 0 <= pos <= size()-1 Requires 0 <= pos <= size()-1

29 Error Conditions null Can null be added? null We’ll ignore adds of null. containsnull contains must handle null correctly. pos Bad pos values will throw IndexOutOfBounds getremove pos get or remove on empty list is really a bad pos error

30

31 Interface definition for ListADT public interface ListADT { void add(E item); void add(int pos, E item); boolean contains(E item); int size( ); boolean isEmpty( ); E get(int pos); E remove(int pos); }

32 Using the ListADT Write a method that reverses the contents of a list. Thus (1,2,3,4) becomes (4,3,2,1). Choose the approach you will take before writing code.

33 One approach: Move 2 nd from right to very end. Then 3 rd from right to very end. … Finally, farthest from right (leftmost) To very end. (11,22,33,44) (11,22,44,33) (11,44,33,22) (44,33,22,11)

34 Java code to reverse a List void reverse(){ for (int i = size() - 2; i >= 0; i--) add(remove(i)); } Why start i at size() - 2 ? Are “corner cases” (lists of size 0 or 1) handled properly?

35 Let’s build a ListAST using an array. Arrays are simple to use but also have a fixed size. We’ll expand the list as necessary when the list grows.

36 public class SimpleArrayList implements ListADT { /* Local data to implement a list*/ /* One or more constructors */ /* Implementations for add, remove, isEmpty, size, get and contains */ }

37 public class SimpleArrayList implements ListADT { private E[] items; private int itemCount; private final int INIT_SIZE; /* One or more constructors */ /* Implementations for interface methods */ }

38 public class SimpleArrayList implements ListADT { private E[] items; private int itemCount; private final int INIT_SIZE; public SimpleArrayList() { itemCount = 0; INIT_SIZE = 100; items = (E[]) new Object[INIT_SIZE]; } /* Implementations for interface methods */ }

39 public class SimpleArrayList implements ListADT { private E[] items; private int itemCount; private final int INIT_SIZE; public SimpleArrayList() {... }... public boolean isEmpty() { return (itemCount == 0); } }

40 public class SimpleArrayList implements ListADT { private E[] items; private int itemCount; private final int INIT_SIZE; public SimpleArrayList() {... }... public int size() { return itemCount; } }

41 public class SimpleArrayList implements ListADT {... public void add(E item) { if (item == null) throw new NullPointerException(); if (itemCount == items.length) expandArray(); items[itemCount] = item; itemCount++; }}

42 public class SimpleArrayList implements ListADT {... private void expandArray() { E[] newArray = (E[]) new Object[itemCount*2]; for (int k = 0; k implements ListADT {... private void expandArray() { E[] newArray = (E[]) new Object[itemCount*2]; for (int k = 0; k < itemCount; k++) { newArray[k] = items[k]; } items = newArray; } }}

43 public class SimpleArrayList implements ListADT { private Object[] items; private int itemCount; private final int INIT_SIZE; public ArrayBag() { … } public boolean isEmpty() { … } public void add(Object item) {…} public Object remove() throws NoSuchElementException { if (itemCount == 0) throw new NoSuchElementException(); else { itemCount--; return items[itemCount] ; } }

44 public class SimpleArrayList implements ListADT {... public void add(int pos, E item){ if (item == null) throw new NullPointerException(); if (itemCount == items.length) expandArray(); if (pos itemCount) throw new IndexOutOfBoundsException(); // move items over and insert new item for (int k = itemCount; k > pos; k--) items[k] = items[k-1]; items[pos] = item; itemCount++; }}

45 public class SimpleArrayList implements ListADT {... public E get(int pos) { // check for bad pos if (pos = itemCount) { throw new IndexOutOfBoundsException(); } // return the item at pos return items[pos]; }; }

46 public class SimpleArrayList implements ListADT {... public E remove(int pos) { // check for bad pos if (pos = itemCount) throw new IndexOutOfBoundsException(); // get the item to be removed from pos E ob = items[pos]; // move items over to fill removed pos for (int k = pos; k implements ListADT {... public E remove(int pos) { // check for bad pos if (pos = itemCount) throw new IndexOutOfBoundsException(); // get the item to be removed from pos E ob = items[pos]; // move items over to fill removed pos for (int k = pos; k < itemCount-1; k++) items[k] = items[k+1]; // decrease the number of items itemCount--; // return the removed item return ob; } }

47 public class SimpleArrayList implements ListADT {... public boolean contains(E item){ // null values are not allowed in the list if (item == null) return false; for (int i=0; i implements ListADT {... public boolean contains(E item){ // null values are not allowed in the list if (item == null) return false; for (int i=0; i < itemCount; i++){ if (items[i].equals(item)) return true; } return false; }}

48 A Very Useful Auxiliary Method In testing and debugging ADTs, it is very useful to see their contents. In Java, you may add a “toString” method to a class. When Java tries to print an object, it calls toString to obtain a string representation of the object, and then prints that string.

49 toString methods need not be very complex to be useful: public String toString() { String result = "("; for (int k = 0; k < itemCount; k++) { if (k==0) result += items[0].toString() ; else result += ("," + items[k].toString()); } return result + ")"; }

50 Are lists of lists allowed? They are, and can be very useful. Consider: SimpleArrayList > LL = new SimpleArrayList >(); SimpleArrayList L3 = new SimpleArrayList (); SimpleArrayList L4 = new SimpleArrayList ();

51 L3.add(123); L3.add(234); L3.add(345); System.out.println(L3); L4.add(456); L4.add(567); System.out.println(L4); LL.add(L3); LL.add(L4); System.out.println(LL); What is printed? (123,234,345) (456,567) ((123,234,345),(456,567))

52 Iterators Most ADTs in Java can provide an iterator object, used to traverse all the data in an ADT.

53 Iterator Interface public interface Iterator { boolean hasNext(); E next(); void remove(); // Optional }

54 Getting an Iterator You get an iterator from an ADT by calling the method iterator(); Iterator iter = myList.iterator();

55 Now a simple while loop can process each data value in the ADT: while(iter.hasNext()) { process iter.next() }

56 A Simple Print method for Lists void printArrayList(){ Iterator iter = this.iterator(); while(iter.hasNext()){ System.out.print(iter.next()+" "); } System.out.println(); }

57 Adding Iterators to SimpleArrayList is easy First, we add the iterator() method to SimpleArrayList : public Iterator iterator(){ return new ArrayListIterator (this) ; }

58 Then we implement the iterator class for Lists: import java.util.*; public class ArrayListIterator implements Iterator { // *** fields *** private SimpleArrayList list; private int curPos; public ArrayListIterator( SimpleArrayList list) { this.list = list; curPos = 0; }

59 public boolean hasNext() { return curPos < list.size(); } public E next() { if (!hasNext()) throw new NoSuchElementException(); E result = list.get(curPos); curPos++; return result; } public void remove() { throw new UnsupportedOperationException(); }

60 Empty vs. Null In Java all objects are accessed through a reference. A reference may be null. This is not the same as a data object that has nothing in it. Consider String str1 = “”; String str2 = null; str1 references something (the empty string) str2 references nothing.


Download ppt "CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18."

Similar presentations


Ads by Google