190 likes | 325 Vues
This chapter provides an overview of collection classes in Java focusing on bags and arrays. It defines the Abstract Data Type (ADT) of a bag, its key operations like add, remove, size, and its dynamic capacity management methods. The chapter also explores the implementation of arrays in Java, including their cloning capabilities. Detailed specifications and methods are presented for the `IntArrayBag` class, covering constructors, methods for adding and removing elements, counting occurrences, and merging collections. This foundational knowledge is essential for managing collections effectively in Java programming.
E N D
Chapter 3Collection Classes Anshuman Razdan Div of Computing Studies razdan@asu.eduhttp://i3dea.asu.edu/razdan
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 CET230 -- Razdan with contribution from others
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 CET230 -- Razdan with contribution from others
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. CET230 -- Razdan with contribution from others
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 CET230 -- Razdan with contribution from others
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; } CET230 -- Razdan with contribution from others
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 } CET230 -- Razdan with contribution from others
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; } CET230 -- Razdan with contribution from others
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++; } CET230 -- Razdan with contribution from others
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; } } CET230 -- Razdan with contribution from others
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; } CET230 -- Razdan with contribution from others
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 index Starting at index Copy to array Copy this many elements CET230 -- Razdan with contribution from others
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; } CET230 -- Razdan with contribution from others
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???? } CET230 -- Razdan with contribution from others
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??? CET230 -- Razdan with contribution from others
trimToSize method public void trimToSize( ) { int trimmedArray[ ]; if (data.length != manyItems) { trimmedArray = new int[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; } } CET230 -- Razdan with contribution from others
getCapacity method public int getCapacity( ) { return data.length; } size method public int size( ) { return manyItems; } CET230 -- Razdan with contribution from others
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 CET230 -- Razdan with contribution from others
Complexity (At Home) CET230 -- Razdan with contribution from others