Presentation is loading. Please wait.

Presentation is loading. Please wait.

(Java Collections Framework) AbstractSequentialList

Similar presentations


Presentation on theme: "(Java Collections Framework) AbstractSequentialList"— Presentation transcript:

1 (Java Collections Framework) AbstractSequentialList
JCF (Java Collections Framework) Collection List {interface} {interface} Java Provides a List interface for several implementations RandomAccess AbstractList {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList 11/28/2018 IT 179

2 ArrayList Method Summary
 boolean add (E o) Appends the specified element to the end of this list.  void add (int index, E element) Inserts the specified element at the specified position in this list. clear () Removes all of the elements from this list. ensureCapacity (int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.  E get (int index) Returns the element at the specified position in this list.  int indexOf (Object e) Searches for the first occurrence of the given argument, testing for equality using the equals method. isEmpty () Tests if this list has no elements. 11/28/2018 IT 179

3 ArrayList Method Summary
remove (int index) Removes the element at the specified position in this list.  boolean remove (Object o) Removes a single instance of the specified element from this list, if it is present (optional operation). set (int index, E element) Replaces the element at the specified position in this list with the specified element.  int size () Returns the number of elements in this list.  Object[] toArray () Returns an array containing all of the elements in this list in the correct order.  void trimToSize () Trims the capacity of this ArrayList instance to be the list's current size. 11/28/2018 IT 179

4 Interface for the user ArrayList LinkedList Vector Stack XXXX List
.... Interface for the user public int size(); public T get(int i); public T set(int i, T item); public int indexOf(T item); public void add(T item); public void add(int i, T item); public T remove(int i); public Object[] toArray(); public <K> K[] toArray(K[] a); 7 2 4 6 1 3 5 8 11/28/2018 IT 179

5 Interface for the user 1 [0] [1] 2 [2] 3 [3] 4 [4] 5 [5] 6 [6] 7 [7] 8
ArrayList< > 1 [0] public int size(); public T get(int i); public T set(int i, T item); public int indexOf(T item); public void add(T item); public void add(int i, T item); public T remove(int i); public Object[] toArray(); public <K> K[] toArray(K[] a); [1] 2 [2] 3 [3] 4 [4] 5 [5] 6 [6] 7 [7] 8 [8] [9] 11/28/2018 IT 179

6 ArrayList class (generic)
import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); L.add(“Good"); L.add(“Afternoon"); L “Good” “Afternoon” 11/28/2018 IT 179

7 What happen behind the scene
import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(8); L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); Good L L Smith Smith Robinson Size=1 Size=2 L Size=0 Capacity =8 Capacity =8 Capacity=8 11/28/2018 IT 179

8 L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good");
L.add(1, "Mrs."); Good Smith Mrs. Robinson L !! Size=2 Size=3 Size=4 Capacity=8 11/28/2018 IT 179

9 L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good");
L.add(1, "Mrs."); L.add(1, "Afternoon"); L.add(2, "!"); Good Afternoon Mrs. Robinson L !! Size=4 Size=5 Capacity=8 IT 179 11/28/2018

10 L …… Good L.add(“Smith"); L.add("Robinson"); L.set(0,"Good");
L.add(1, "Mrs."); L.add(1, "Afternoon"); L.add(2, "!"); Good Afternoon Morning ! L.set(1, "Morning"); Mrs. L Robinson !! Size=6 Capacity=8 11/28/2018 11/28/2018 IT 179

11 AbstractSequentialList
The Java Collections Framework (JCF) Collection List {interface} {interface} Java Provides a List interface for several implementations RandomAccess Iterable AbstractList {interface} {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList 11/28/2018 IT 179

12 Need for Iterators O(n) The internal information
1 5 iterA 2 4 3 3 4 2 5 3 iterB 1 6 2 7 1 8 iterC while (iter.hasNext()) { ..... iter.next()..... } The internal information and structures are protected. 8 6 7 4 O(n) 1 3 2 5 11/28/2018 IT 179

13 AbstractList<T>
Iterable<T> {interface} + Iterator<T> iterator(); AbstractList<T> {interface} (see the java document) …. is-relation has-relation is-relation Iterator<T> {interface} + boolean hasNext(); + T next(); + void remove(); ArrayList<T> 11/28/2018 IT 179

14 API java.util.Iterator<E>
Interface (usually for some internal class) Iterator<E> bool hasNext(); E next(); void remove(); import java.util.Iterator; ..... static ArrayList<Card> pick(char suit, PokerDeck deck) { ArrayList<Card> a = deck.toArrayList(); Iterator<Card> aiter = a.iterator(); while (aiter.hasNext()) { if (aiter.next().getSuit() != suit) aiter.remove(); } return a; 11/28/2018 IT 179

15 Ace Finding Game H2 H13 H3 H4 H5 H7 H12 H10 H6 H1 H8 H9 H11
11/28/2018 IT 179


Download ppt "(Java Collections Framework) AbstractSequentialList"

Similar presentations


Ads by Google