150 likes | 269 Views
Lists and ArrayList. many slides taken from Mike Scott, UT Austin. Description of Lists. Lists maintain items in a definite order Items can be added normally at the end in any valid location? access items in the list first, last, any valid location? delete items any valid item?.
E N D
Lists and ArrayList many slides taken from Mike Scott, UT Austin
Description of Lists • Lists maintain items in a definite order • Items can be added • normally at the end • in any valid location? • access items in the list • first, last, any valid location? • delete items • any valid item?
List Interface • Design a list interface with all of the basic operations and then add optional operations you feel are appropriate:
Implementing a List • After designing interface next step is to complete an implementation • What should be used as the internal storage container?
The Default Add Method • Default add usually means add to the end of the list. • public boolean add(Object item)
Big O of the Default add? • Normal Big O analysis fails • "Most" adds are inexpensive • O(1) • A few adds are very expensive due to resizing • O(N) • What is the average case? • To determine average case we'll do an amortized analysis • amortized is a term from accounting. Spread the cost of something over time.
Simplified Amortized Analysis • Add N items to the list, always at end of list • Assume internal container starts out at size 1 • Assume if we resize container we double the size Item # Size of Work to Work to Container Resize Add 1 1 0 1 2 1 -> 2 1 1 3 2 -> 4 2 1 4 4 0 1 5 4 -> 8 4 1 6 8 0 1 7 8 0 1 8 8 0 1 9 8 -> 16 8 1 …N - 1 N - 1 0 1 N N - 1 -> 2N - 2 N - 1 1
Total Work to Add N items • The work in resizing is the sum of a geometric sequence 1 + 2 + 4 + 8 + 16 + … + (N-1)/2 + N-1 an = a1 * r n - 1 sum terms 1 through n = (r * an - a1)/ (r - 1)a1 = 1, an = N - 1, r = 2, sum = (2 * (N - 1) - 1) / (2 - 1) = 2N - 3
Average Work Per Item • Total Work is N + 2N - 3 = 3N - 3 • Divide by the N items to amortize the cost: (3N - 3) / N items = 3 - 3/N steps per item • The average work per item is 3 - 3/N • The amortized Big O is O(1) • What would the amortized Big O of adding at the end of the list be if we resized the list by 100 elements every time we needed to resize? • 100+200+300+…+n=100(1+2+…+n/100)=?
Adding at an Arbitrary Location in List • public void add(Object item, int position)
The contains method • public boolean contains(Object item)
An addAll method • public void addAll(ArrayList other)
Insertion • In operation insertAtRank(r, o), we need to make room for the new element by shifting forward the n - r elements V[r], …, V[n -1] • In the worst case (r =0), this takes O(n) time V 0 1 2 n r V 0 1 2 n r V o 0 1 2 n r
Deletion V 0 1 2 n r V 0 1 2 n r V o 0 1 2 n r • In operation removeAtRank(r), we need to fill the hole left by the removed element by shifting backward the n - r -1 elements V[r +1], …, V[n -1] • In the worst case (r =0), this takes O(n) time
From the Iterator interface • Method Summary: • public boolean hasNext() • Returns true if the iteration has more elements. • public Object next() • Returns the next element in the iteration. • public void remove() • Removes from the underlying collection the last element returned by the iterator (optional operation). • Iterators allow a programmer to access all elements in a Collection object, one at a time, without having to worry about underlying implementation: • For addAll(Collection other) we are only interested in hasNext and next