1 / 15

Lists and ArrayList

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?.

Download Presentation

Lists and ArrayList

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists and ArrayList many slides taken from Mike Scott, UT Austin

  2. 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?

  3. List Interface • Design a list interface with all of the basic operations and then add optional operations you feel are appropriate:

  4. Implementing a List • After designing interface next step is to complete an implementation • What should be used as the internal storage container?

  5. The Default Add Method • Default add usually means add to the end of the list. • public boolean add(Object item)

  6. 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.

  7. 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

  8. 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

  9. 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)=?

  10. Adding at an Arbitrary Location in List • public void add(Object item, int position)

  11. The contains method • public boolean contains(Object item)

  12. An addAll method • public void addAll(ArrayList other)

  13. 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

  14. 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

  15. 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

More Related