Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Collection Classes Anshuman Razdan Department of Engineering

Similar presentations


Presentation on theme: "Chapter 3 Collection Classes Anshuman Razdan Department of Engineering"— Presentation transcript:

1 Chapter 3 Collection Classes Anshuman Razdan Department of Engineering razdan@asu.edu http://i3dea.asu.edu/razdan

2 CET230 -- Razdan with contribution from others 2 Collection Classes Collection Class – an ADT in which each object contains a number of elements. –Bag – a container that holds an unordered collection of elements –Sequence – a container that holds an ordered collection of elements

3 CET230 -- Razdan with contribution from others 3 Bag ADT Specification Constructor(s) – can specify capacity of bag Add – put something in the bag Remove – take something out of the bag Size – tells how many things in the bag ensureCapacity – make sure the bag can hold a given number of items trimToSize – make the capacity of the bag exactly the number of items currently in the bag countOccurrences – counts how many of a given item are in the bag addAll – dump one bag into another Union – create new bag with same contents as two other bags Clone – returns a “copy” of a bag

4 CET230 -- Razdan with contribution from others 4 Review Arrays Arrays are Java objects –int[] nums = new int[4]; –int[] nums = new int[] {1,2,3,72}; The length attribute (NOT method) tells the number of indices in the array Arrays have a clone method that will return a “copy” (clone) of the array.

5 CET230 -- Razdan with contribution from others 5 Implementing a clone method The purpose of a clone method is to create a copy of an object Cloneable is a java interface. If we want to implement a clone method: –Modify class head to public class MyClass implements Cloneable –Use super.clone to make a copy using clone of “parent” –Add any additional implementation to make copy

6 CET230 -- Razdan with contribution from others 6 Generic clone method The clone method should follow a format similar to (see page 85 of textbook): public Object clone(){ MyObject answer; try{ answer = (MyObject) super.clone(): } catch( CloneNotSupportedException e ){ throw new RuntimeException (“This class does not implement Cloneable.”); } // any additional code for making a copy return answer; }

7 CET230 -- Razdan with contribution from others 7 IntArrayBag Class Public class IntArrayBag implements Cloneable { private int[] data; // array to store collection // of integers private int manyItems; // current capacity of bag // public methods }

8 CET230 -- Razdan with contribution from others 8 Constructors public IntArrayBag( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = new int[INITIAL_CAPACITY]; } public IntArrayBag(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException ("The initialCapacity is negative: " + initialCapacity); data = new int[initialCapacity]; manyItems = 0; }

9 CET230 -- Razdan with contribution from others 9 Add method public void add(int element) { if (manyItems == data.length) { // Double the capacity and add 1; this works even if manyItems is 0. // However, in the case that manyItems*2 + 1 is beyond // Integer.MAX_VALUE, there will be an arithmetic overflow and // the bag will fail. ensureCapacity(manyItems*2 + 1); // why do we have } // to do this???? data[manyItems] = element; manyItems++; }

10 CET230 -- Razdan with contribution from others 10 Remove method public boolean remove(int target) { int index; // The location of target in the data array. // First, set index to location of target in data array, which could be as small // as 0 or as large as manyItems-1; If target is not in array, index will be manyItems; for (index = 0; (index < manyItems) && (target != data[index]); index++) // No work is needed in the body of this for-loop. ; if (index == manyItems) // target not found, so nothing is removed. return false; else { // target found at data[index]; reduce manyItems and copy last element onto data[index]. manyItems--; data[index] = data[manyItems]; return true; }

11 CET230 -- Razdan with contribution from others 11 countOccurrences method public int countOccurrences(int target) { int answer; int index; answer = 0; for (index = 0; index < manyItems; index++) if (target == data[index]) answer++; return answer; }

12 CET230 -- Razdan with contribution from others 12 addAll method public void addAll(IntArrayBag addend) { // If addend is null, then a NullPointerException is thrown. // In the case that the total number of items is beyond // Integer.MAX_VALUE, there will be an arithmetic overflow and // the bag will fail. ensureCapacity(manyItems + addend.manyItems); System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems); manyItems += addend.manyItems; } Copy from array Starting at indexCopy to array Starting at index Copy this many elements

13 CET230 -- Razdan with contribution from others 13 Union method public static IntArrayBag union(IntArrayBag b1, IntArrayBag b2) { // If either b1 or b2 is null, then a NullPointerException is thrown. // In the case that the total number of items is beyond // Integer.MAX_VALUE, there will be an arithmetic overflow and // the bag will fail. IntArrayBag answer = new IntArrayBag(b1.getCapacity( ) + b2.getCapacity( )); System.arraycopy(b1.data, 0, answer.data, 0, b1.manyItems); System.arraycopy(b2.data, 0, answer.data, b1.manyItems, b2.manyItems); answer.manyItems = b1.manyItems + b2.manyItems; return answer; }

14 CET230 -- Razdan with contribution from others 14 Clone method public Object clone( ) { // Clone a IntArrayBag object. IntArrayBag answer; try { answer = (IntArrayBag) super.clone( ); } catch (CloneNotSupportedException e) { throw new RuntimeException ("This class does not implement Cloneable"); } answer.data = (int [ ]) data.clone( ); // why do we have return answer; // to do this???? }

15 CET230 -- Razdan with contribution from others 15 ensureCapacity method public void ensureCapacity(int minimumCapacity) { int biggerArray[ ]; if (data.length < minimumCapacity) { biggerArray = new int[minimumCapacity]; System.arraycopy(data, 0, biggerArray, 0, manyItems); data = biggerArray; } Why not initialize earlier???

16 CET230 -- Razdan with contribution from others 16 trimToSize method public void trimToSize( ) { int trimmedArray[ ]; if (data.length != manyItems) { trimmedArray = new int[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; }

17 CET230 -- Razdan with contribution from others 17 getCapacity method public int getCapacity( ) { return data.length; } size method public int size( ) { return manyItems; }

18 CET230 -- Razdan with contribution from others 18 Complexity Analysis Determine run-time for each Bag method, assume the following: –Bag b1 has n1 elements & capacity c1 –Bag b2 has n2 elements & capacity c2 –Methods operating on a single bag, write in terms of b1 –Methods operating on two bags, use both b1 and b2

19 CET230 -- Razdan with contribution from others 19 Complexity (At Home) MethodTime AnalysisMethodTime Analysis Constructor (0 param) Constructor (1 param) addaddAll unionremove countOccurrencesclone sizegetCapacity ensureCapacitytrimToSize


Download ppt "Chapter 3 Collection Classes Anshuman Razdan Department of Engineering"

Similar presentations


Ads by Google