Collection List interface JCF interface Java Collections Framework

  • Slides: 15
Download presentation
Collection List {interface} JCF {interface} (Java Collections Framework) Abstract. List Java Provides a List

Collection List {interface} JCF {interface} (Java Collections Framework) Abstract. List Java Provides a List interface for several implementations Random. Access {interface} Array. List {abstract} Vector Abstract. Sequenti al. List {abstract} Stack 9/30/2020 IT 179 Linked. List 1

Array. List Method Summary boolean add (E o) Appends the specified element to the

Array. List 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. void clear () Removes all of the elements from this list. void ensure. Capacity (int min. Capacity) Increases the capacity of this Array. List 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 index. Of (Object e) Searches for the first occurrence of the given argument, testing for equality using the equals method. boolean is. Empty () Tests if this list has no elements. 9/30/2020 IT 179 2

Array. List Method Summary E remove (int index) Removes the element at the specified

Array. List Method Summary E 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). E 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[] to. Array () Returns an array containing all of the elements in this list in the correct order. void trim. To. Size () Trims the capacity of this Array. List instance to be the list's current size. 9/30/2020 IT 179 3

Array. List Linked. List Vector Stack XXXX List. . . . Interface for the

Array. List Linked. List Vector Stack XXXX List. . . . Interface for the user public int size(); 7 public T get(int i); 2 public T set(int i, T item); public int index. Of(T item); public void add(int i, T item); 4 6 1 3 5 public T remove(int i); public Object[] to. Array(); 8 public <K> K[] to. Array(K[] a); 9/30/2020 IT 179 4

Interface for the user Array. List< > [0] public int size(); [1] public T

Interface for the user Array. List< > [0] public int size(); [1] public T get(int i); public T set(int i, T item); public int index. Of(T item); 3 4 [4] [5] public void add(int i, T item); [6] public T remove(int i); [7] public Object[] to. Array(); [8] [9] IT 179 2 [3] public void add(T item); public <K> K[] to. Array(K[] a); 9/30/2020 [2] 1 5 6 7 8 5

Array. List class (generic) import java. util. Array. List; . . . Array. List<String>

Array. List class (generic) import java. util. Array. List; . . . Array. List<String> L = new Array. List<String>(); L. add(“Good"); L. add(“Afternoon"); L “Good” “Afternoon” 9/30/2020 IT 179 6

What happen behind the scene import java. util. Array. List; . . . Array.

What happen behind the scene import java. util. Array. List; . . . Array. List<String> L = new Array. List<String>(8); L. add(“Smith"); L. add("Robinson"); L. set(0, "Good"); … … L Good L L Smith Robinson Size=1 Size=2 Capacity =8 Size=0 Capacity=8 9/30/2020 IT 179 7

…… L. add(“Smith"); L. add("Robinson"); L. set(0, "Good"); L. add("!!"); L. add(1, "Mrs. ");

…… 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 9/30/2020 IT 179 8

…… L. add(“Smith"); L. add("Robinson"); L. set(0, "Good"); L. add("!!"); L. add(1, "Mrs. ");

…… 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 9/30/2020 IT 179 9

…… L. add(“Smith"); L. add("Robinson"); L. set(0, "Good"); L. add("!!"); L. add(1, "Mrs. ");

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

Collection {interface} List {interface} The Java Collections Framework (JCF) Java Provides a List interface

Collection {interface} List {interface} The Java Collections Framework (JCF) Java Provides a List interface for several implementations Random. Access {interface} Array. List 9/30/2020 Abstract. List {abstract} Iterable {interface} Vector Abstract. Sequential. List {abstract} Stack Linked. List IT 179 11

Need for Iterators 1 5 4 iter. A 2 3 3 4 2 5

Need for Iterators 1 5 4 iter. A 2 3 3 4 2 5 3 iter. B 1 6 2 7 1 iter. C while (iter. has. Next()) {. . iter. next(). . . } 8 6 7 4 O(n) 9/30/2020 8 The internal information and structures are protected. 1 3 2 5 IT 179 12

Iterable<T> {interface} is-relation Abstract. List<T> {interface} (see the java document) …. + Iterator<T> iterator();

Iterable<T> {interface} is-relation Abstract. List<T> {interface} (see the java document) …. + Iterator<T> iterator(); has-relation is-relation Iterator<T> {interface} Array. List<T> + boolean has. Next(); + T next(); + void remove(); 9/30/2020 IT 179 13

API java. util. Iterator<E> Interface (usually for some internal class) bool has. Next(); E

API java. util. Iterator<E> Interface (usually for some internal class) bool has. Next(); E next(); void remove(); import java. util. Iterator; . . . static Array. List<Card> pick(char suit, Poker. Deck deck) { Array. List<Card> a = deck. to. Array. List(); Iterator<Card> aiter = a. iterator(); while (aiter. has. Next()) { if (aiter. next(). get. Suit() != suit) aiter. remove(); } return a; } 9/30/2020 IT 179 14

Ace Finding Game 1. H 2 H 13 H 4 H 5 H 7

Ace Finding Game 1. H 2 H 13 H 4 H 5 H 7 H 12 H 10 H 6 H 1 H 8 H 9 H 11 2. H 13 H 2 H 3 H 4 H 5 H 7 H 12 H 10 H 6 H 1 H 8 H 9 H 11 3. H 11 H 9 H 8 H 1 H 6 H 10 H 12 H 7 H 5 H 4 H 3 H 2 H 13 4. H 3 H 4 H 5 H 7 H 12 H 10 H 6 H 1 H 8 H 9 H 11 H 2 H 13 5. H 5 H 4 H 3 H 7 H 12 H 10 H 6 H 1 H 8 H 9 H 11 H 2 H 13 6. H 12 H 7 H 3 H 4 H 5 H 10 H 6 H 1 H 8 H 9 H 11 H 2 H 13 7. H 2 H 11 H 9 H 8 H 1 H 6 H 10 H 5 H 4 H 3 H 7 H 12 H 13 8. H 11 H 2 H 9 H 8 H 1 H 6 H 10 H 5 H 4 H 3 H 7 H 12 H 13 9. H 7 H 3 H 4 H 5 H 10 H 6 H 1 H 8 H 9 H 2 H 11 H 12 H 13 10. H 1 H 6 H 10 H 5 H 4 H 3 H 7 H 8 H 9 H 2 H 11 H 12 H 13 9/30/2020 IT 179 15