150 likes | 234 Views
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. ArrayList
E N D
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 IT 179
ArrayList LinkedList Vector Stack XXXX List .... .... Interface for the user publicint size(); public T get(int i); public T set(int i, T item); publicint indexOf(T item); publicvoid add(T item); publicvoid 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 IT 179
Interface for the user ArrayList< > 1 [0] publicint size(); public T get(int i); public T set(int i, T item); publicint indexOf(T item); publicvoid add(T item); publicvoid 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] IT 179
ArrayList class (generic) import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); L.add(“Good"); L.add(“Afternoon"); L “Good” “Afternoon” IT 179
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 IT 179
…… L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add("!!"); L.add(1, "Mrs."); Good Smith Mrs. Robinson L !! Size=2 Size=3 Size=4 Capacity=8 IT 179
…… L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add("!!"); L.add(1, "Mrs."); L.add(1, "Afternoon"); L.add(2, "!"); Good Afternoon Mrs. Robinson L !! Size=4 Size=5 Capacity=8 IT 179
…… L.add(“Smith"); L.add("Robinson"); L.set(0,"Good"); L.add("!!"); 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 1/4/2020 IT 179
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 IT 179
Need for Iterators 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 IT 179
is-relation has-relation is-relation IT 179
Iterator<E> Interface (usually for some internal class) bool hasNext(); E next(); void remove(); API java.util.Iterator<E> 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; } IT 179
H2 H13 H3 H4 H5 H7 H12 H10 H6 H1 H8 H9 H11 • H13 H2 H3 H4 H5 H7 H12 H10 H6 H1 H8 H9 H11 • H11 H9 H8 H1 H6 H10 H12 H7 H5 H4 H3 H2 H13 • H3 H4 H5 H7 H12 H10 H6 H1 H8 H9 H11 H2 H13 • H5 H4 H3 H7 H12 H10 H6 H1 H8 H9 H11 H2 H13 • H12 H7 H3 H4 H5 H10 H6 H1 H8 H9 H11 H2 H13 • H2 H11 H9 H8 H1 H6 H10 H5 H4 H3 H7 H12 H13 • H11 H2 H9 H8 H1 H6 H10 H5 H4 H3 H7 H12 H13 • H7 H3 H4 H5 H10 H6 H1 H8 H9 H2 H11 H12 H13 • H1 H6 H10 H5 H4 H3 H7 H8 H9 H2 H11 H12 H13 Ace Finding Game IT 179