Topic 2: Array-Based Lists
Topic 2: Array-Based Lists
Topic 2: Array-Based Lists
Array-Based Lists
Learning Outcomes
At the end of the class, students are able to List different types of List data structure Implement various List operations, such as search, insert, remove etc. design and implement a generic class to process various types of lists
Array-Based Lists
List: A collection of elements of the same type Length of list is number of elements in list
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 DataElement, superclass of every class specifying data type of list elements Abstract methods of class DataElement: equals, compareTo, makeCopy, and getCopy
7
class IntElement
(Malik, 2003)
10
class StringElement
(Malik, 2003)
11
class ArrayListClass
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 maxSize: specifies the maximum number of elements that can be processed by the list list: an array of reference variables
12
class ArrayListClass
13
14
Definition of ArrayListClass
public abstract class ArrayListClass { protected int length; //to store the length //of the list protected int maxSize; //to store the maximum //size of the list protected DataElement[] list; //array to hold //list elements //Place the definitions of the instance // methods and abstract methods here.
21
Unordered List
class UnorderedArrayList is a subclass of the class ArrayListClass Elements are not necessarily sorted class UnorderedList implements operations search, insert, and remove
22
class UnorderedArrayList
23
Search
Necessary components to search a list:
Array containing the list Length of the list Item for which you are searching
Search
public int seqSearch(DataElement searchItem) { int loc; boolean found = false; for(loc = 0; loc < length; loc++) if(list[loc].equals(searchItem)) { found = true; break; } if(found) return loc; else return -1; }//end seqSearch
25
Remove
deletes an item from the list uses the methods seqSearch and removeAt to remove an item from the list
26
Insert
public void insert(DataElement insertItem) { int loc; if(length == 0) //list is empty list[length++] = insertItem; //insert the item and //increment the length else if(length == maxSize) System.err.println(Cannot insert in a full list.); else { loc = seqSearch(insertItem); if(loc == -1) //the item to be inserted //does not exist in the list list[length++] = insertItem.getCopy(); else System.err.println(The item to be inserted is + already in the list. No + duplicates are allowed.); } }//end insert 27
Remove
public void remove(DataElement removeItem) { int loc; if(length == 0) System.err.println(Cannot delete from an empty list.); else { loc = seqSearch(removeItem); if(loc != -1) removeAt(loc); else System.out.println(The item to be deleted is + not in the list.); } }//end remove
28
29
31
32
Topic Summary
Operations performed on a list Type of list elements Abstract class DataElement Classes IntElement, DoubleElement, StringElement class ArrayListClass
Definitions of Nonabstract Methods of ArrayListClass Definition of ArrayListClass
33
Topic Summary
Unordered List Class UnorderedArrayList
Implementations of search, insert and remove
34