![Page 1: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/1.jpg)
Bag Implementations that Use Arrays
Chapter 2
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Data Structures and Abstractions with Java, 4e Frank Carrano
![Page 2: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/2.jpg)
Fixed-Size Array to Implement the ADT Bag
FIGURE 2-1 A classroom that contains desks in fixed positions
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 3: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/3.jpg)
Fixed-Size Array
FIGURE 2-2 UML notation for the class ArrayBag, including the class’s data fields
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 4: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/4.jpg)
Fixed-Size Array
LISTING 2-1 An outline of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 5: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/5.jpg)
Fixed-Size Array
LISTING 2-1 An outline of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 6: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/6.jpg)
Fixed-Size Array
LISTING 2-1 An outline of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 7: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/7.jpg)
Fixed-Size Array
FIGURE 2-3 Adding entries to an array that represents a bag, whose capacity is six, until it becomes full
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 8: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/8.jpg)
Fixed-Size Array
FIGURE 2-3 Adding entries to an array that represents a bag, whose capacity is six, until it becomes full
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 9: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/9.jpg)
Fixed-Size Array
Method add© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 10: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/10.jpg)
Fixed-Size Array
Method isFull
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 11: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/11.jpg)
Fixed-Size Array
Method toArray
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 12: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/12.jpg)
Making the Implementation Secure
• Practice fail-safe programming by including checks for anticipated errors
• Validate input data and arguments to a method• refine incomplete implementation of ArrayBag
to make code more secure by adding the following two data fields
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 13: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/13.jpg)
Making the Implementation Secure
Revised constructor
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 14: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/14.jpg)
Making the Implementation Secure
Method to check initialization
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 15: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/15.jpg)
Making the Implementation Secure
Revise the method add
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 16: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/16.jpg)
Testing the Core Methods
Stubs for remove and clear
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 17: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/17.jpg)
Testing the Core Methods
LISTING 2-2 A program that tests core methods of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 18: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/18.jpg)
Testing the Core Methods
LISTING 2-2 A program that tests core methods of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 19: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/19.jpg)
Testing the Core Methods
LISTING 2-2 A program that tests core methods of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 20: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/20.jpg)
Testing the Core Methods
LISTING 2-2 A program that tests core methods of the class ArrayBag
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 21: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/21.jpg)
Implementing More Methods
Methods isEmpty and getCurrentSize
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 22: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/22.jpg)
Implementing More Methods
Method getFrequencyOf
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 23: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/23.jpg)
Implementing More Methods
Method contains
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 24: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/24.jpg)
Methods That Remove Entries
The method clear
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 25: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/25.jpg)
Methods That Remove Entries
The method remove
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 26: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/26.jpg)
Methods That Remove Entries
FIGURE 2-4 The array bag after a successful search for the string “Nancy"
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 27: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/27.jpg)
Methods That Remove Entries
FIGURE 2-5 (a) A gap in the array bag after setting the entry in bag[index] to null; (b) the array after shifting
subsequent entries to avoid a gap© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 28: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/28.jpg)
Methods That Remove Entries
FIGURE 2-6 Avoiding a gap in the array while removing an entry
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 29: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/29.jpg)
Methods That Remove Entries
New definition of remove
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 30: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/30.jpg)
Methods That Remove Entries
The second remove method
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 31: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/31.jpg)
Methods That Remove Entries
The removeEntry method
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 32: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/32.jpg)
Methods That Remove Entries
Definition for the method getIndexOf© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 33: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/33.jpg)
Methods That Remove Entries
Revised definition for the method contains© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 34: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/34.jpg)
Using Array Resizing
FIGURE 2-7 Resizing an array copies its contents to a larger second array
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 35: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/35.jpg)
Using Array Resizing
FIGURE 2-8 (a) An array; (b) two references to the same array; (c) the original array variable now references a new,
larger array;© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 36: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/36.jpg)
Using Array Resizing
FIGURE 2-8 (d) the entries in the original array are copied to the new array; (e) the original array is discarded
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 37: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/37.jpg)
Using Array Resizing
FIGURE 2-9 The effect of the statement myArray = Arrays.copyOf(myArray, 2 * myArray.length);
(a) The argument array; (b) the parameter that references the argument array; (c) a new, larger array that gets the contents of the argument array; (d) the return value that references the new array; (e) the argument variable is
assigned the return value
© 2015 Pearson Education, Inc., Upper Sa ddle River, NJ. All rights reserved.
![Page 38: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/38.jpg)
New Implementation of a Bag
Previous definition of method add
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 39: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/39.jpg)
New Implementation of a Bag
Revision of method doubleCapacity
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 40: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/40.jpg)
Pros and Cons of Using an Array
• Adding an entry to the bag is fast
• Removing an unspecified entry is fast
• Removing a particular entry requires time to locate the entry
• Increasing the size of the array requires time to copy its entries
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
![Page 41: Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions](https://reader034.vdocument.in/reader034/viewer/2022051621/5697bf871a28abf838c8871c/html5/thumbnails/41.jpg)
End
Chapter 2
© 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.