Chapter 10: An Array Instance Variable

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
Java's Collection Framework
1 The Map ADT © Rick Mercer. 2 The Map ADT  A Map is an abstract data type where a value is "mapped" to a unique key  Also known as Dictionary  Need.
1 Java's Collection Framework By Rick Mercer with help from The Java Tutorial, The Collections Trail, by Joshua BlockThe Collections Trail.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
The Java Collections Framework (Part 2) By the end of this lecture you should be able to: Use the HashMap class to store objects in a map; Create objects.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
1 Interfaces in Java’s Collection Framework Rick Mercer.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
An Array-Based Implementation of the ADT List
Using the Java Collection Libraries COMP 103 # T2
Lecture 10 Collections Richard Gesick.
The List ADT.
Intro To Classes Review
The Singly-Linked Structure
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Rick Mercer, Allison Obourn, Marty Stepp
A Bag Implementation that Links Data
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Stacks.
Building Java Programs
Building Java Programs
EE 422C Sets.
Chapter 11 Generic Collections
Lecture 2: Implementing ArrayIntList reading:
Object Oriented Programming in java
Podcast Ch18b Title: STree Class
Chapter 7 The Java Array Object © Rick Mercer.
OO Design with Inheritance
Stacks.
ArrayLists 22-Feb-19.
Collections Framework
List Implementation via Arrays
Java Programming Language
CSC 143 Java Linked Lists.
Data Structures & Algorithms
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Recursive Objects Singly Linked Lists.
Podcast Ch21f Title: HashSet Class
slides created by Ethan Apter and Marty Stepp
CS100A Lect. 12, 8 Oct More About Arrays
Presentation transcript:

Chapter 10: An Array Instance Variable Asserting Java Rick Mercer

A Collection Class Programmers often use collection classes classes with the main purpose of storing a collection of elements that can be treated as a single unit For example, class Bank might store a collection of BankAccount objects and provide appropriate access to the individual elements A Few Java collection class names: Stack a collection of objects with access to the one on top ArrayList a collection of objects with the List operations TreeSet a collection of unique objects HashMap collection to provide fast adds, removes, finds

Characteristics of a collection class Main responsibility: store a collection of objects Can add and remove objects from the collection Provides access to individual elements May have search-and-sort operations Some collections allow duplicate elements (ArrayList), other collections do not (class Set for example) Some collections have a natural ordering--OrderedSet--other collections do not--ArrayList

What about arrays? Is an array a collection? arrays are objects, they do have similar characteristics, however subscripts are needed to access individual elements programmers have to spend a lot of time implementing array based adds, removes, sorts, and searches arrays are lower level Arrays provides programmers with the opportunity to make more more errors and spend more time than using a collection class

class StringBag The StringBag class represents a mathematical bag or multi-set is a simple collection class will have an array instance variable is a collection capable of a storing only strings elements actually references to string objcets is not a "standard", but this allows us to see the inner working of the class

A StringBag class continued A StringBag object stores a collection of String elements that are not in any particular order are not necessarily unique understands these messages add occurencesOf remove

StringBag with no implementation // A class for storing a multi-set (bag) of String elements. public class StringBag { // Construct an empty StringBag object (no elements yet) public StringBag() { } // Add a string to the StringBag in no particular place. public void add(String stringToAdd) { // Return how often element equals one in this StringBag public int occurencesOf(String element) { return 0; // remove first element that equals elementToRemove public boolean remove(String elementToRemove) { return false;

A test method for add and occurencesOf public void testAddAndOccurencesOf() { StringBag sb = new StringBag(); sb.add("Marlene"); sb.add("Eric"); assertEquals(3, sb.occurencesOf("Marlene")); assertEquals(2, sb.occurencesOf("Eric")); assertEquals(0, sb.occurencesOf("Not here")); }

Implement StringBag methods The constructor creates an empty StringBag no elements in it, size is 0 public StringBag() { size = 0; data = new String[20]; }

StringBag add The StringBag.add operation adds all new elements to the "end" of the array if there is "room" public void add(String stringToAdd) { // If there is no more room, do nothing // Otherwise, place at end of array } could we have added stringToAdd at the beginning?____?

StringBag occurencesOf The occurencesOf method returns how often the argument equals a StringBag element public int occurencesOf (String value) { }

StringBag remove If the element is found, StringBag remove uses sequential search to find the element to be removed (arbitrallily use the first when occurencesOf > 1 If the element is found, move the last element data[size-1] into the location of the removal element place null into where the last element was done to release the memory for garbage collection decrease size by 1

State of s1 before removing "Jignesh" Array Data Field State data[0] "Kelly" data[1] "Jignesh" data[2] "Kristen" data[3] "Maya" data[4] null ... null size 4 local objects in StringBag remove removalCandidate "Jignesh" subscript 1

The state after removing "another string" 1. Find removalCandidate in data[1] 2. Overwrite data[1] with "Maya" (the last element and decrease size by 1 data[0] "Kelly" data[1] "Maya "Jignesh" data[2] "Kristen" data[3] "Maya" ... size 3 Erase reference to "Jignesh" No longer meaningful Decrease number of elements in the bag

StringBag remove calls a private helper method indexOf public boolean remove(String stringToRemove) { boolean result = false; // Get subscript of stringToRemove or -1 if not found int subscript = indexOf(stringToRemove); if(subscript != -1) { // Move the last string in the array to // where stringToRemove was found. data[subscript] = data[size-1]; // Release that memory to be reused for any object data[size-1] = null; // And then decrease size by one size--; // return true to where the message was sent result = true; } return result;

Code Demo: Complete StringBag remove that shifts all elements public boolean remove(String stringToRemove) { // Get subscript of stringToRemove or -1 if not found int subscript = indexOf(stringToRemove); if(subscript < 0) return false; else { // Shift all elements left so this array { "Kelly", "Jignesh", "Kristen", "Maya", null, null} // would change to this { "Kelly", "Kristen", "Maya", null, null, null} // don't forget to return true and reduce size } 16