1 / 15

Collection

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

ktussey
Télécharger la présentation

Collection

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. 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

  2. IT 179

  3. IT 179

  4. 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

  5. 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

  6. ArrayList class (generic) import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); L.add(“Good"); L.add(“Afternoon"); L “Good” “Afternoon” 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 IT 179

  8. …… 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

  9. …… 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

  10. …… 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

  11. 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

  12. 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

  13. is-relation has-relation is-relation IT 179

  14. 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

  15. 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

More Related