List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

List Implementations That Use Arrays
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists Chapter 8 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
List Implementations That Use Arrays Chapter 5 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Stack Implementations Chapter Chapter Contents A Linked Implementation An Array-Based Implementation A Vector-Based Implementation.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
 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.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
April 24, 2017 Chapter 4 Data Abstraction The Walls
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.
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.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Array-Based Implementations Chapter 3 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Sorted Lists Chapter Chapter Contents Specifications for the ADT Sorted List Using the ADT Sorted List A Linked Implementation The Method add The.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Lists and Sorted Lists: various implementations Slides by Nadia Al-Ghreimil adopted from Steve Armstrong LeTourneau University Longview, TX  2007, 
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Lists Chapter 4.
Bag Implementations that Use Arrays
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Adapted from Pearson Education, Inc.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Dynamic Data Structures and Generics
List Implementations that Use Arrays
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
ArrayLists 22-Feb-19.
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Recitation 2 CS0445 Data Structures
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

List Implementations That Use Arrays Chapter 5

2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation Using Dynamic Array Expansion to Implement the ADT List Expanding an Array A New Implementation of a List Using a Vector to Implement the ADT List A Summary of Methods in the Class Vector The Pros and Cons of Using an Array to Implement the ADT List The Class ArrayList The Interface Serializable

3 An Analogy Consider a classroom with 40 desks in fixed position Desks are wasted if less than 40 students Not enough desks if more than 40 students An array is like the classroom Each desk an array location

4 An Analogy Fig. 5-1 A classroom that contains desks in a fixed position.

5 An Analogy Suppose we have some students in classroom in order alphabetically We add a new student We desire to maintain the alphabetic order We must shift some students We remove a student in the middle of the sequence Again, we must shift some students

6 Adding a Student Fig. 5-2 Seating a new student between two existing students: at least one other student must move

7 The Java Implementation Private data fields for implementation of AList Implements interface ListInterface of Chapter 4 Note the full specification, pgs private Object entry[]; // array of list entries private int length; // current number of entries in list private static final int MAX_SIZE = 50; // max length of list

8 AList Implementation (1): public class AList implements ListInterface { private T[] entry; // array of list entries private int length; // current number of entries in list private static final int MAX_SIZE = 50; // max length of list public AList() { this(MAX_SIZE); } // end default constructor public AList(int maxSize) { length = 0; entry = (T[]) new Object[maxSize]; } // end constructor public boolean add(Object newEntry) { /* */ } // end add

9 AList Implementation (2): public boolean add(int newPosition, T newEntry) { /* */ } // end add public T remove(int givenPosition) { /* */ } // end remove public void clear() { length = 0; // } // end clear public boolean replace(int givenPosition, T newEntry) { /* */ } // end replace public T getEntry(int givenPosition) { /* */ } // end getEntry

10 AList Implementation (3): public boolean contains(T anEntry) { /* */ } // end contains public int getLength() { return length; } // end getLength public boolean isEmpty() { return length == 0; } // end isEmpty public boolean isFull() { return length == entry.length; } // end isFull

11 AList Implementation (4): public void display() { for (int index = 0; index < length; index++) System.out.println(entry[index]); } // end display /* < This class will define two private methods that will be discussed later. > */ } // end AList

12 AList add() Methods First add method adds a new item at the end of the list Assign new value at end Increment length of list Second add method adds item in mid-list Requires a utility method, makeRoom() This shifts elements ahead See my Eclipse Code

13 Adding Items in Mid-list Fig. 5-3 Making room to insert Carla as third entry in an array.

14 The remove() Method Must shift existing entries to avoid gap in the array Except when removing last entry Method must also handle error situation When position specified in the remove is invalid When remove() is called and the list is empty Invalid call returns null value

15 Removing a List Entry Fig. 5-4 Removing Bob by shifting array entries.

16 Dynamic Array Expansion An array has a fixed size If we need a larger list, we are in trouble When array becomes full Move its contents to a larger array (dynamic expansion) Copy data from original to new location Manipulate names so new location keeps name of original array

17 Dynamic Array Expansion Fig. 5-5 The dynamic expansion of an array copies the array's contents to a larger second array.

18 Dynamic Array Expansion Fig. 5-6 (a) an array; (b) the same array with two references; (c) the two arrays, reference to original array now referencing a new, larger array

19 A New Implementation of a List Change the isFull to always return false We will expand the array when it becomes full We keep this function so that the original interface does not change The add() methods will double the size of the array when it becomes full Now declare a private method isArrayFull Called by the add() methods

20 A new add() method (1): public boolean isFull() { return false; } private boolean isArrayFull() { return length == list.length; } public boolean add(T newEntry) { if (isArrayFull()) doubleArray(); // add new entry after last current entry entry[length] = newEntry; length++; return true; } // end add

21 A new add() method (2): NOTE: See System.arraycopy() for built-in array copy method. /** Task: Doubles the size of the array of list entries. */ private void doubleArray() { T[] oldList = entry; // save reference to array of // list entries int oldSize = oldList.length; // save old max size of array entry = (T[]) new Object[2*oldSize]; // double size of array // copy entries from old array to new, bigger array for (int index = 0; index < oldSize; index++) entry[index] = oldList[index]; } // end doubleArray

22 Using a Vector to Implement the ADT List Java's Vector class provides capabilities of an array Able to expand dynamically Hides the details of the process Vector Found in package java.util Has methods for manipulating entries Enables implementing the ADT List

23 Using a Vector Fig. 5-7 A client uses the methods given in ListInterface, but the implementation of the list uses Vector methods to perform its operations

24 Using a Vector Elements of the class Class Vector comes from package java.util Data field entry is an instance of a Vector import java.util.Vector; public class VectorList implements ListInterface { private Vector entry; // entries in list...

25 Using a Vector The add() methods The first uses the addElement method from the Vector class The other uses the insertElementAt method The remove() method Uses the removeElementAt method

26 Pros and Cons of Array Use for the ADT List When using an array or vector … Retrieving an entry is fast Adding an entry at the end of the list is fast Adding or removing an entry that is between other entries requires shifting elements in the array Increasing the size of the array or vector requires copying elements

27 Java Class Library Has a class similar to class AList defined in this chapter Class ArrayList Uses dynamic array expansion Interface Serializable Represent an object as a sequence of bytes to be written to a file Add the words implements Serializable to class definition public class AList implements ListInterface, Serializable {...

28 Serializable This is an interface with no methods. Read the ADT specification. Read the last part of Appendix C of our text.