Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 205 – Java Programming II Lecture 25 March 8, 2002.

Similar presentations


Presentation on theme: "CSC 205 – Java Programming II Lecture 25 March 8, 2002."— Presentation transcript:

1 CSC 205 – Java Programming II Lecture 25 March 8, 2002

2 List

3 The List Interface List represents an ordered collection (also known as a sequence). The List Interface implements the Collec- tion interface. The user of this interface –has precise control over where in the list each element is inserted. –can access elements by their integer index (position in the list) – can search for elements in the list.

4 List Operations The List interface provides four methods for positional (indexed) access to list elements. // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be // thrown. // Postcondition: the element at position index has // been returned. Object get (int index);

5 List Operations // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: element has replaced the element that // was at index before this call, and that // previous occupant has been returned. Object set (int index, Object element);

6 List Operations // Precondition: 0 <= index <= size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: element has been inserted at position // index and every element that was at a // position >= index before this call is now at // the next higher position. void add (int index, Object element);

7 List Operations // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: the element that was at position index // in this List before this call has been // removed, every element that was in a // position > index before this call is now // at the next lower position, and the // removed element has been returned. Object remove (int index);

8 List Operations The List interface provides two methods to search for a specified object // Precondition: 0 <= index < size( ). Otherwise, // IndexOutOfBoundsException will be thrown. // Postcondition: if elem does occur in this List, the index // of the first occurrence of elem has been // returned. Otherwise, -1 has been returned. int indexOf (Object elem); and, similarly int lastIndexOf (Object elem);

9 The ListIterator Interface The List interface provides a special iterator, called a ListIterator, that allows –element insertion and replacement –bi-directional access A method is provided to obtain a list iterator that starts at a specified position in the list.  public ListIterator listIterator()ListIterator  public ListIterator listIterator(int ind)ListIterator In addition to the hasNext and next methods, this interface provides hasPrevious and previous methods also

10 Hierarchical Structure

11 The AbstractList Class Provides a skeletal implementation of the List interface –To implement an unmodifiable list, the programmer needs only to extend this class and provide implementations for  get(int index)  size() –To implement a modifiable list, override  set(int index, Object element) –If the list is of a variable-size, override  add(int index, Object element)  remove(int index)

12 The ArrayList Class Resizable-array implementation of the List interface. –implements all optional list operations, –permits all elements, including null Provides methods to manipulate the size of the array that is used internally to store the list Roughly equivalent to Vector

13 ArrayList – An Overview ArrayList : a classy array Random access of an element from its index –public Object get(int index) –public void set(int index, Object element) Insertion or removal of an element at a given index –public void add(int index, Object element) –public Object remove(int index) Automatically resizing after invoking –public void add(int index, Object element) –public boolean add(Object o)

14 ArrayList – Performance The following operations run in constant time –size, isEmpty, get, set, iterator, and listIterator The add operation runs in amortized constant time, –that is, adding n elements requires O(n) time. All of the other operations run in linear time –With the constant factor is low compared to that for the LinkedList implementation.

15 ArrayList – Capacity Each ArrayList instance has a capacity. –the size of the array used to store the elements in the list As elements are added an ArrayList, its capacity grows automatically. –before adding a large number of elements using the ensureCapacity operation.  public void ensureCapacity(int minCapacity)

16 ArrayList – User’s View 1. public ArrayList (int initialCapacity) 2. public ArrayList( ) 3. public ArrayList (Collection c) 4. public boolean add(Object o 5. public void add (int index, Object element) 6. public boolean addAll(Collection c) 7. public boolean addAll(int ind, Collection c) 8. public void clear( ) 9. public Object clone( ) 10. public boolean contains (Object elem) 11. public boolean containsAll (Collection c)

17 ArrayList – User’s View 12. public void ensureCapacity(int minCapacity) 13. public boolean equals(Object o) 14. public Object get(int index) 15. public int hashCode( ) 16. public int indexOf (Object elem) 17. public boolean isEmpty( ) 18. public Iterator iterator( ) 19. public int lastIndexOf (Object elem) 20. public ListIterator listIterator( ) 21. public ListIterator listIterator (final int index) 22. public boolean remove (Object o)

18 ArrayList – User’s View 23. public Object remove (int index) 24. public boolean removeAll(Collection c) 25. public boolean retainAll(Collection c) 26. public Object set(int index, Object element) 27. public int size( ) 28. public List subList(int fromInd, int toInd) 29. public Object[ ] toArray( ) 30. public Object[ ] toArray (Object[ ] a) 31. public String toString() 32. public void trimToSize( )

19 Group Exercise Constructs an ArrayList that holds up to n elements public ArrayList(int initialCapacity); In a loop with i going from 0 to n – 1, appends new Double(i) to the ArrayList public boolean add(Object o); Inserts new Double(1.4) at index n/3 public void add(int index, Object element);

20 Group Exercise  Removes the element at index 2*n/3 public Object remove (int index); Multiplies the middle element by 3.5 public Object get(int index); public Object set(int index, Object element); Data conversion myD = new Double (3.14); // double -> Double double d = myD.doubleValue( ); // Double -> double


Download ppt "CSC 205 – Java Programming II Lecture 25 March 8, 2002."

Similar presentations


Ads by Google