150 likes | 359 Views
CSC 212 – Data Structures. Lecture 28 : Array-Based List. List ≠ array. List at times resembles concept of array, but … … no preset size of a List In List, index where element found may change Could implement an List using: Array Linked list Administrator/Faculty
E N D
CSC 212 – Data Structures Lecture 28:Array-Based List
List ≠ array • List at times resembles concept of array, but… • … no preset size of a List • In List, index where element found may change • Could implement an List using: • Array • Linked list • Administrator/Faculty • Other Faculty Members
ArrayListConcept (Usually) • Will also see this called Vector & IndexList • Used interchangeably; depends on where you look • Based upon aconceptual resizable array • Arbitrary sequence of elements stored within list • IndexListkeeps elements indexed from 0 to n-1 • Rank is meaningless; only gives position in list
ArrayListIndices (Usually) • Index used by ArrayListto organize elements • List uses index of 0 for item at front • 2nd item at index 1in the IndexList • IndexListstores 3rd item at index 2 • nth item at index n-1in the IndexList • ArrayListstores elements in sequential indices • Index is absolute position of element in the list • Indices must be sequential & cannot be blank • Cannot be repeated; 1 element per index at a time
Something About List’s Methods • contains()checks if elementalready included • Requires comparing with elements currently in List • remove()removes element IF in List already • Requires comparing with elements currently in List • add()keeps order by placing element correctly OR • addAfter()places element after finding target • Requires comparing with elements currently in List
What They Have In Common • All methods first search through the List • Searching should differ if List ordered or unordered • Once item found (or not found) differences occur • Couldrewrite code…… but violates laziness goal • Use private method that returns index where found • All (public) methods then rely upon this private one
How To Solve This? • How we go about search will differ by List type • Search all elements in List when data unordered • Only search through smaller elements if ordered • Couldrewrite all public methods in all classes…… still violates laziness goal • Make private method abstractin List superclass • Means that cannot use superclass (lacking add methods) • Override method in subclasses where approach known
What About add*() Methods? • Code similar among these methods, too • All of these methods must make space in array • Identical updates to needed fields by each of these • Would still like to avoid having to duplicate code • Difference is WHERE space will be needed • addFirst() & addRear()work at List’s ends • Middle of List used (usually) by add() & addAfter() • Create protected addAt()method in superclass • Specify element & index as parameters to this method
Insertion • To make room for e, addAt()shifts elements • Move to higher indices in array to make room • Process requires O(n) time • Copyelements at index i & higher for space in array 0 1 2 n-1 i
Deletion • remove() “shifts” elements down to fill hole • Remember: not all array locations contain elements • Copy elements down from indices holding data 0 1 2 e n-1 i
Array-based IndexList • Do not want size limits restricting size of object • That this has no preset size is main advantage • ADT hides complexity of changing array’s size • When calling add()with array full • Create larger array and copy values into it • Larger array aliased by field… • …without revealing secret to anyone
Ways to Grow Array • Two ways to increase array size • Constant value (e.g., 2, 4, 6, 8, 10…) • Constant factor (e.g., 2, 4, 8, 16, 32…) • Both approaches requires O(n) time… • Instantiating & copying array are slow steps • …average (amortized) costs differ, however • Difference in how often slow step needed
Your Turn • Get into your groups and complete activity
For Next Lecture • Read section 6.7 for Monday’s lecture • How do LinkedList & a linked list differ? • What is advantage of LinkedListover ArrayList? • Week #10 assignment posted to Angel • As usual, will be due next Tuesday • Enjoy respite from crushing deadlines during lull • Get ready to move on; project #2 available in one week