Chapter 3 ArrayBased Lists Data Structures Using Java
Chapter 3 Array-Based Lists Data Structures Using Java 1
Chapter Objectives • Learn about lists • Explore how various operations, such as search, insert, and remove, on lists are implemented • Learn how to design and implement a generic class to process various types of lists • Become aware of the class Vector Data Structures Using Java 2
Array-Based Lists • List: A collection of elements of the same type • Length of list is number of elements in list Data Structures Using Java 3
Operations Performed on a List • • • Create the list: initialize to an empty state Determine whether the list is empty Determine whether the list is full Find the size of the list Destroy (clear) the list Determine whether an item is the same as a given list element Data Structures Using Java 4
Operations Performed on a List • Insert an item in the list at the specified location • Remove an item from the list at the specified location • Replace an item at the specified location with another item • Retrieve an item from the list at the specified location • Search the list for a given item Data Structures Using Java 5
Type of List Elements • class Object directly or indirectly becomes a superclass of every Java class, user defined or built in • Reference variable of Object type can refer to any object of any class • class Data. Element, superclass of every class specifying data type of list elements • Abstract methods of class Data. Element: equals, compare. To, make. Copy, and get. Copy Data Structures Using Java 6
Type of List Elements Data Structures Using Java 7
Type of List Elements • class Int. Element used when list of integers is manipulated • class Double. Element used when list of decimal numbers is manipulated • class String. Element, designed in chapter, used when list of strings is manipulated Data Structures Using Java 8
class Int. Element Data Structures Using Java 9
class String. Element Data Structures Using Java 10
class Array. List. Class • An abstract class • Is superclass of the classes that implement a list • Has three instance variables – length: specifies the number of elements currently in the list – max. Size: specifies the maximum number of elements that can be processed by the list – list: an array of reference variables Data Structures Using Java 11
class Array. List. Class Data Structures Using Java 12
Definitions of Nonabstract Methods of Array. List. Class public boolean is. Empty() public int list. Size() { { return length; return (length == 0); } } public boolean is. Full() public int max. List. Size() { { return max. Size; return (length == max. Size); } } Data Structures Using Java 13
Definitions of Nonabstract Methods of Array. List. Class public void print() { for(int i = 0; i < length; i++) System. out. print(list[i] + “ “); System. out. println(); } public boolean is. Item. At. Equal(int location, Data. Element item) { return (list[location]. equals(item)); } Data Structures Using Java 14
Definitions of Nonabstract Methods of Array. List. Class public void insert. At(int location, Data. Element insert. Item) { if(location < 0 || location >= max. Size) System. err. println(“The position of the item to “ + “be inserted is out of range”); else if(length >= max. Size) //list is full System. err. println(“Cannot insert in a full list. ”); else { for(int i = length; i > location; i--) list[i] = list[i - 1]; //move the elements down list[location] = insert. Item. get. Copy(); //insert the //item at the specified position length++; //increment the length } }//end insert. At Data Structures Using Java 15
Definitions of Nonabstract Methods of Array. List. Class public void insert. End(Data. Element insert. Item) { if(length >= max. Size) //the list is full System. err. println(“Cannot insert in a full list. ”); else { list[length] = insert. Item. get. Copy(); //insert item //at end length++; //increment the length } }//end insert. End Data Structures Using Java 16
Definitions of Nonabstract Methods of Array. List. Class public void remove. At(int location) { if(location < 0 || location >= length) System. err. println(“The location of the item to “ + “be removed is out of range. ”); else { for(int i = location; i < length - 1; i++) list[i] = list[i + 1]; list[length - 1] = null; length--; } }//end remove. At Data Structures Using Java 17
Definitions of Nonabstract Methods of Array. List. Class public Data. Element retrieve. At(int location) { if(location < 0 || location >= length) { System. err. println("The location of the item to be " + "retrieved is out of range. "); return null; } else return list[location]. get. Copy(); }//end retrieve. At Data Structures Using Java 18
Definitions of Nonabstract Methods of Array. List. Class public void replace. At(int location, Data. Element rep. Item) { if(location < 0 || location >= length) System. err. println(“The location of the item to “ + “be replaced is out of range. ”); else list[location]. make. Copy(rep. Item); }//end replace. At public void clear. List() { for(int i = 0; i < length; i++) list[i] = null; length = 0; System. gc(); }//end clear. List Data Structures Using Java 19
Definition of Array. List. Class public abstract class Array. List. Class { protected int length; //to store the length //of the list protected int max. Size; //to store the maximum //size of the list protected Data. Element[] list; //array to hold //list elements //Place the definitions of the instance // methods and abstract methods here. } Data Structures Using Java 20
Unordered List • class Unordered. Array. List is a subclass of the class Array. List. Class • Elements are not necessarily sorted • class Unordered. List implements operations search, insert, and remove Data Structures Using Java 21
class Unordered. Array. List Data Structures Using Java 22
Search • Necessary components to search a list: – Array containing the list – Length of the list – Item for which you are searching • After search completed: – If item found, report “success”, return location in array – If item not found, report “failure” Data Structures Using Java 23
Search public int seq. Search(Data. Element search. Item) { int loc; boolean found = false; for(loc = 0; loc < length; loc++) if(list[loc]. equals(search. Item)) { found = true; break; } if(found) return loc; else return -1; }//end seq. Search Data Structures Using Java 24
Insert and Remove • Insert – Inserts a new item in the list – Uses method seq. Search to determine whether insert. Item is already in list • Remove – deletes an item from the list – uses the methods seq. Search and remove. At to remove an item from the list Data Structures Using Java 25
Insert public void insert(Data. Element insert. Item) { int loc; if(length == 0) //list is empty list[length++] = insert. Item; //insert the item and //increment the length else if(length == max. Size) System. err. println(“Cannot insert in a full list. ”); else { loc = seq. Search(insert. Item); if(loc == -1) //the item to be inserted //does not exist in the list[length++] = insert. Item. get. Copy(); else System. err. println(“The item to be inserted is “ + “already in the list. No “ + “duplicates are allowed. ”); } }//end insert Data Structures Using Java 26
Remove public void remove(Data. Element remove. Item) { int loc; if(length == 0) System. err. println(“Cannot delete from an empty list. ”); else { loc = seq. Search(remove. Item); if(loc != -1) remove. At(loc); else System. out. println(“The item to be deleted is “ + “not in the list. ”); } }//end remove Data Structures Using Java 27
Time Complexity of List Operations Data Structures Using Java 28
Vectors • Class Vector can be used to implement a list • Unlike array, size of Vector object can grow/shrink during program execution • Do not need to worry about number of data elements in vector Data Structures Using Java 29
Members of the class Vector • protected int element. Count; • protected Object[] element. Data; • • public Vector() public Vector (int size) public void add. Element (Object insert. Obj) public void insert. Element. At (Object insert. Obj, int index) public Object clone ( ) public boolean contains (Object obj) Data Structures Using Java 30
Members of the class Vector • • • public void copy. Into (Object[] dest) public Object element. At (int index) public Object first. Element () public Object last. Element () public int index. Of (Object obj, int index) public boolean is. Empty () public int last. Index. Of (Object obj) Data Structures Using Java 31
Members of the class Vector • • public int last. Index. Of (Object item, int index) public void remove. All. Elements ( ) public boolean remove. Element (Object obj) public void remove. Element. At (int index) public void set. Element. At (Object obj, int index) public int size ( ) public String to. String ( ) Data Structures Using Java 32
Vectors • Every element of Vector object is reference variable of type Object • To add element into Vector object – Create appropriate object – Store data into object – Store address of object holding data into Vector object element Data Structures Using Java 33
Vector String. List string. List. add. Element(“Spring”); string. List. add. Element(“Summer”); string. List. add. Element(“Fall”); string. List. add. Element(“Winter”); Data Structures Using Java 34
Vector String. List string. List. add. Element(“Cool”, 1); Data Structures Using Java 35
Programming Example: Polynomials Purpose: To design and implement the class Polynomial to perform various polynomial operations in a program Program implements the following polynomial operations: 1. Evaluate a polynomial at a given value 2. Add polynomials 3. Subtract polynomials 4. Multiply polynomials Data Structures Using Java 36
Programming Example: Polynomials Data Structures Using Java 37
Programming Example: Polynomials Data Structures Using Java 38
Chapter Summary • • Operations performed on a list Type of list elements Abstract class Data. Element Classes Int. Element, Double. Element, String. Element • class Array. List. Class – Definitions of Nonabstract Methods of Array. List. Class – Definition of Array. List. Class Data Structures Using Java 39
Chapter Summary • Unordered List • Class Unordered. Array. List – Implementations of search, insert and remove • Time Complexity of List Operations • Vectors – Members of the class Vector • Programming examples Data Structures Using Java 40
- Slides: 40