1 / 188

Chapter 2

Chapter 2. Lists and the Collections Framework. Chapter Objectives. The List interface Writing an array-based implementation of List Linked list data structures: Singly-linked Doubly-linked Circular Big-O notation and algorithm efficiency

helena
Download Presentation

Chapter 2

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. Chapter 2 Lists and theCollections Framework

  2. Chapter Objectives • The List interface • Writing an array-based implementation of List • Linked list data structures: • Singly-linked • Doubly-linked • Circular • Big-O notation and algorithm efficiency • Implementing the List interface as a linked list • The Iterator interface • Implementing Iterator for a linked list • Testing strategies • The Java Collections framework (hierarchy)

  3. Introduction • A list • a collection of elements, each with a position or index • Iterators • facilitate sequential access to a list • Classes ArrayList, Vector, andLinkedList • subclasses of abstract class AbstractList • implement the List interface

  4. The List Interface and ArrayList Class Section 2.1

  5. Array, Java Array object • Array - an indexed structure • Random access • Sequential access • Java Array object • Fixed length • Hard to change its length • add an element • remove an element

  6. ListInterface • part of JAVA API java.util • List operations: • Obtain an element at a specified position • Replace an element at a specified position • Find a specified target value • Add an element at either end • Remove an element from either end • Insert or remove an element at any position • Traverse the list structure without managing a subscript

  7. java.util.ListInterface and its Implementers

  8. ArrayListClass • Implement List interface • It cannot store primitive types • Classes must store values as objects • How to store primitive types?

  9. ArrayListClass, … more • An improvement over an array object • adding new elements to the end of a list • access elements quickly in any order

  10. Example List<String> myList = new ArrayList<String>(); • Initial value - empty • initial capacity -10 elements • add elements to “myList”, myList.add("Bashful"); myList.add("Awful"); myList.add("Jumpy"); myList.add("Happy");

  11. Add in between • Adding an element with subscript 2: myList.add(2, "Doc"); • Notice that the subscripts of "Jumpy" and "Happy" have changed from [2],[3] to [3],[4]

  12. Add at the end myList.add("Dopey");

  13. Remove an element • Removing an element: myList.remove(1); • The strings referenced by [2] to [5] have changed to [1] to [4]

  14. Replace an element myList.set(2, "Sneezy");

  15. Access an element • myList[1]? • OR String dwarf = myList.get(1);

  16. Get index of an element myList.indexOf("Sneezy"); Returns? myList.indexOf("Jumpy"); Returns?

  17. Generic Collections • The statement List<String> myList = new ArrayList<String>(); • Generic collections or Generics • “String” -- type parameter • It sets the data type of all objects stored in a collection

  18. Generic Collections: Primitive type example • General declaration CollectionClassName<E> variable = new CollectionClassName<E>(); • “E” is a type parameter • Adding a noncompatible type • How about primitive types ? ArrayList<Integer> myList = new ArrayList<Integer>(); myList.add(new Integer(3)); // ok myList.add(3); // also ok! 3 is automatically wrapped in an Integer object myList.add(new String("Hello")); // error!

  19. Why Use Generic Collections? • Flexible • List<T>  List<int>, or List<string>,… • Enhance “type-checking” List v = new ArrayList(); v.add("test"); Integer i = (Integer)v.get(0); // Run time error List<String> v = new ArrayList<String>(); v.add("test"); Integer i = v.get(0); // (type error) Compile time error

  20. ArrayList: Methods

  21. Applications of ArrayList Section 2.2

  22. Application of ArrayList ArrayList<Integer> someInts = new ArrayList<Integer>(); int[] nums = {5, 7, 2, 15}; for (inti = 0; i < nums.length; i++) { someInts.add(nums[i]); } // Display the sum int sum = 0; for (inti = 0; i < someInts.size(); i++) { sum += someInts.get(i); } System.out.println("sum is " + sum);

  23. The Example ArrayList<Integer> someInts = new ArrayList<Integer>(); int[] nums = {5, 7, 2, 15}; for (int i = 0; i < nums.length; i++) { someInts.add(nums[i]); } // Display the sum int sum = 0; for (int i = 0; i < someInts.size(); i++) { sum += someInts.get(i); } System.out.println("sum is " + sum); nums[i] is an int; it is automatically wrapped in an Integer object

  24. Another Application:Phone Directory public class DirectoryEntry { String name; String number; } Create a class for objects stored in the directory

  25. Phone Directory Example public class DirectoryEntry { String name; String number; } private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>(); Create the directory

  26. Add an Entry Add a DirectoryEntry object public class DirectoryEntry { String name; String number; } private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>(); theDirectory.add(new DirectoryEntry("Jane Smith", "555-1212"));

  27. Search Phone Directory by “Name” Method indexOf searches theDirectory by applying the equals method for class DirectoryEntry. Assume DirectoryEntry'sequals method compares name fields. public class DirectoryEntry { String name; String number; } private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>(); theDirectory.add(new DirectoryEntry("Jane Smith", "555-1212")); int index = theDirectory.indexOf(new DirectoryEntry(aName, ""));

  28. Access Phone Directory by “index” public class DirectoryEntry { String name; String number; } private ArrayList<DirectoryEntry> theDirectory = new ArrayList<DirectoryEntry>(); theDirectory.add(new DirectoryEntry("Jane Smith", "555-1212")); int index = theDirectory.indexOf(new DirectoryEntry(aName, "")); if (index != -1) dE = theDirectory.get(index); else dE = null;

  29. Implementation of an ArrayList Class Section 2.3

  30. Fields: Capacity and Size • KWArrayList: • Capacity: Physical size • Size: Number of data items stroed

  31. KWArrayListFields import java.util.*; /** This class implements some of the methods of the Java ArrayList class */ public class KWArrayList<E> { // Data fields /** The default initial capacity */ private static final int INITIAL_CAPACITY = 10; /** The underlying data array */ private E[] theData; /** The current size */ private int size = 0; /** The current capacity */ private int capacity = 0; }

  32. KWArrayListConstructor public KWArrayList () { capacity = INITIAL_CAPACITY; theData = (E[]) new Object[capacity]; } This statement allocates storage for an array of type Object(superclass for all types)and then casts the array object to type E[] Although this may cause a compiler warning, it's ok

  33. ImplementingArrayList.add(E) • Two add methods • append at the end of the list • insert an item at a specified position

  34. Add at the end • Check the size • insert the new item at the “size” position • size=size++

  35. Implementing Add in betweenArrayList.add(intindex,EanEntry) • Check size • size = size + 1 • Make room first • Insert at “index” position

  36. ImplementingArrayList.add(index,E) public void add (int index, E anEntry) { // check bounds if (index < 0 || index > size) { throw new ArrayIndexOutOfBoundsException(index); } // Make sure there is room if (size >= capacity) { reallocate(); } // shift data for (int i = size; i > index; i--) { theData[i] = theData[i-1]; } // insert item theData[index] = anEntry; size++; }

  37. setandgetMethods public E get (int index) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } return theData[index]; } public E set (int index, E newValue) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } E oldValue = theData[index]; theData[index] = newValue; return oldValue; }

  38. removeMethod: idea • Check “index” • Shift left • size = size - 1

  39. removeMethod: the code public E remove (int index) { if (index < 0 || index >= size) { throw new ArrayIndexOutOfBoundsException(index); } E returnValue = theData[index]; for (int i = index + 1; i < size; i++) { theData[i-1] = theData[i]; } size--; return returnValue; }

  40. reallocateMethod • Create a new array with double size • Copy the contents of the new array private void reallocate () { capacity *= 2; theData = Arrays.copyOf(theData, capacity); }

  41. reallocateMethod: doubling private void reallocate () { capacity *= 2; theData = Arrays.copyOf(theData, capacity); } The reason for doubling is to spread out the cost of copying; we discuss this further in the next section

  42. KWArrayListas a Collection of Objects: old version • Earlier versions of Java did not support generics; all collections contained only Objectelements • To implement KWArrayListthis way, • remove the parameter type<E>from the class heading, • replace each reference to data type E by Object • The underlying data array becomes private Object[] theData;

  43. Vector/ArrayListClass • Vector and ArrayList are similar • ArrayList is more efficient • Vector class is synchronized • Support multiple threads access a Vector object without confliction

  44. Algorithm Efficiency and Big-O Section 2.4

  45. Algorithm Efficiency/Big-O • the performance of an algorithm • Hard to measure precisely • Big-O notation is a choice • Big-O notation • Function of the size of a problem • Used to compare the algorithms’ efficiency

  46. Linear Growth Rate • The processing time is proportional to the problem size • the algorithm grows at a linear rate public static int search(int[] x, int target) { for(int i=0; i < x.length; i++) { if (x[i]==target) return i; } return -1; // target not found }

  47. Linear Growth Rate • If the target is not present, the for loop will execute x.length times • If the target is present the for loop will execute (on average) (x.length + 1)/2 times • Therefore, the total execution time is directly proportional to x.length • This is described as a growth rate of order n OR • O(n) public static int search(int[] x, int target) { for(int i=0; i < x.length; i++) { if (x[i]==target) return i; } return -1; // target not found }

  48. n x m Growth Rate • Processing time can be dependent on two different inputs public static booleanareDifferent(int[] x, int[] y) { for(int i=0; i < x.length; i++) { if (search(y, x[i]) != -1) return false; } return true; }

  49. n x m Growth Rate (cont.) • The for loop will execute x.length times • But it will call search, which will execute y.length times • The total execution time is proportional to (x.length * y.length) • The growth rate has an order of n x m or • O(n x m) public static booleanareDifferent(int[] x, int[] y) { for(int i=0; i < x.length; i++) { if (search(y, x[i]) != -1) return false; } return true; }

  50. Quadratic Growth Rate • If n==m, thethe algorithm grows at a quadratic rate public static boolean areUnique(int[] x) { for(int i=0; i < x.length; i++) { for(int j=0; j < x.length; j++) { if (i != j && x[i] == x[j]) return false; } } return true; }

More Related