# Lists

Télécharger la présentation

## Lists

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Lists Ellen Walker CPSC 201 Data Structures Hiram College

2. What is a List (abstract)? • Sequence of objects • Objects are in order • There is a first object • There is a last object • Each object (except last) has a successor • Each object (except first) has a predecessor • List can be changed • Objects can be added (list grows) • Objects can be removed (list shrinks)

3. Some Operations on a List • Construct a new list • Add an element • Indicate where (beginning, end, middle?) • Remove an element • Indicate which one (how?) • Find an element in the list • Check whether the list is empty or not • (etc)

4. List vs. Array • List is more general than array • List is abstract, array is an implementation • List is more flexible than array • List can grow or shrink; array is fixed size • List contains Objects, array can contain base types • Use wrappers (e.g. Integer) with boxing/unboxing in a List

5. List Class Hierarchy

6. Choosing an Implementation • Any implementation will implement all methods of the interface (by definition) • But • Some implementations might be more efficient at some operations • Example: • Removing element from middle of list • ArrayList: shift all elements to fill the “hole” - O(N) • LinkedList: adjust successor of element before the removed one O(1)

7. ArrayList Implementation • The list is stored in an array, which is a private member of the ArrayList (you cannot access it) • This array has a capacity • When the number of elements in the list exceeds the capacity, the internal array is replaced by a bigger one

8. Constructing an ArrayList • ArrayList() • Constructs initially empty list, capacity of 10 • ArrayList(int capacity) • Constructs initially empty list with given capacity • Example: Both lists are empty (size of 0), but A has capacity of 10, and B has capacity of 5 • ArrayList A = new ArrayList(); • ArrayList B = newArrayList(5);

9. Specifying Element Types • Generics - specify element type in < > //Construct an ArrayList that holds strings List<String> myList = new ArrayList<String> (); //An ArrayList of Circles List<Circle> myList = new ArrayList<Circle>(); • Without type specification, Object is assumed • Backwards compatible with Java before 5.0

10. Why Generic Collections? • Old-style needs casting, compiler cannot check • List myList = new ArrayList(); • myList.add(new Integer(5)); //any Object is OK • System.out.println((String) myList.get(0)); //runtime error: cannot cast Integer to String • Generic - Compiler error • List myList<String> = new ArrayList<String>(); • myList.add(new Integer(5)); //compiler error - type mismatch

11. Why Generic Collections? • Old-style: Objects must be explicitly wrapped List List1 = new ArrayList(); List1.add(5); //error • Generic: Autoboxing takes care of it List<Integer> List1 = new ArrayList<Integer>(); List1.add(5); //new Integer(5) created and inserted

12. Adding Elements • For an ArrayList<E> • ArrayList<String> rainbow = new ArrayList<String> (5); • add (E obj) - adds obj to the end of the list • rainbow.add(“green”); • rainbow.add(“violet”); • add (int index, E obj) - adds obj at position index of the list • rainbow.add(0, “red”) • rainbow.add(2, “blue”);

13. Accessing and Changing Elements • Use get() and set() instead of [ ] for ArrayLists • More general (works for other list types) • Get method • aColor = rainbow.get(1); • for(int c=0;c<rainbow.size();c++) System.out.println(rainbow.get(c)); • Set method • rainbow.set(1, “Crimson”);

14. Summary: Java ArrayList

15. More examples • What add statements are needed to complete the rainbow? (red, orange, yellow, green, blue, indigo, violet) • What statement will change “indigo” to “purple”? • What will rainbow.indexOf(“orange”) return? • What will rainbow.indexOf(“elephant”) return?

16. Application vs. Implementation • We now know enough to apply the ArrayList class, but… • To really understand an ADT, we should learn about its implementation • We will implement classes similar to each of the collection classes we study • ArrayList vs. KWArrayList • LinkedList vs. KWLinkedList …

17. Implementation of an ArrayList Class • KWArrayList: simple implementation of a ArrayList class • Physical size of array indicated by data field capacity • Number of data items indicated by the data field size

18. Implementation of an ArrayList Class (continued)

19. Overview of KWArrayList • Data Members • Private static final int INITIAL_CAPACITY = 10; • Private E[ ] theData; //array for data • Private int size = 0; //current size • Private int capacity = 0; //current capacity • Default constructor • Sets capacity and creates an array for theData

20. Overview of KWArrayList (cont) • Public boolean add (E anEntry) • Check if the array is full; if so then reallocate • theData[size] = anEntry; • Increment size • Public boolean add (int index, E anEntry) • Check if the array is full; if so then reallocate • Shift elements beyond index to make room • theData[index] = anEntry • Increment size

21. Overview of KWArrayList (cont) • Public E remove(int index) • Shift all items after index left by one space • Decrement size • Return the element that was removed • Private void reallocate () • Double the capacity • Create a new array of the appropriate size • Copy all the data into the new array

22. Cost of Reallocation • Every time we reallocate, we copy capacity entries into the new array (of size 2*capacity) • Cost is proportional to the capacity (C*capacity) • Since we reallocate only after adding capacity items we have: • Capacity -1 additions (0 extra cost) • 1 addition (C*capacity extra cost) • Averaging it out, the extra cost is constant per addition

23. Performance of KWArrayList • Set and get methods execute in constant time • Inserting or removing elements is linear time • What is worst case of removing an element? • What is worst case of inserting an element? (Hint: consider capacity of the list) • IndexOf is linear time