1 / 22

Two types of arrays

Two types of arrays. Static array size determined at compile time can’t change afterwards Dynamic array (JAVA’s type, objects) Size determine at run time Can be resized (larger or smaller). The Java Collections Framework (JCF). Collection. List. {interface}. {interface}.

riona
Download Presentation

Two types of arrays

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. Two types of arrays • Static array • size determined at compile time • can’t change afterwards • Dynamic array (JAVA’s type, objects) • Size determine at run time • Can be resized (larger or smaller) IT 179

  2. The Java Collections Framework (JCF) Collection List {interface} {interface} RandomAccess Java Provides a List interface for several implementations AbstractList {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList IT 179

  3. Tow kinds of Lists Array Linked List IT 179

  4. Array Characteristics Array Linked List A • The elements of an array are in adjacent memory locations • An array is a random (direct) access data structure. A[0] A[1] A[2] A[3] IT 179

  5. What can be done? What can be easily done? Some typical operations on Lists Array Linked List • Random access • Add/remove from the head • Add/remove from the tail • Add/remove from the middle • Resize IT 179

  6. Operations on Array • Traversing – can be bidirectional O(n) • Resizing – O(size of new array)operation • Replacing an element – an O(1) operation • Inserting an element – an O(n) operation due to data movement • Deleting an element – O(1) or O(n) depending on implementation IT 179

  7. Some other operations on arraysare expensive (way more than you thought) • Resizing – O(size of new array)operation • Inserting an element – an O(n) operation due to data movement • Deleting an element – O(1) or O(n) depending on implementation IT 179

  8. Resizing – O(size of new array)operation 4  8 But, why not O(size of old array)? This may not be movable or difficult to move IT 179

  9. Resizing – O(size of new array)operation 4  8 IT 179

  10. Inserting an element – O(n) Deleting an element – O(n) Delete 7 6 6 ? Resizing operation is needed IT 179

  11. Speedup the three slow operations on Array Resizing, Insertion, Deletion Size of the array Capacity of the array Java provides a class called ArrayListthat implements this idea IT 179

  12. import java.util.ArrayList; ..... ArrayList<String> L = new ArrayList<String>(); ArrayList<Robot> r = new ArrayList<Robot>(100); ArrayList<Student> itk179 = new ArrayList<Student>(50); IT 179

  13. IT 179

  14. IT 179

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

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

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

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

  19. …… 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 ITK 179 19 10/8/2014 IT 179

  20. The Java Collections Framework (JCF) Collection List {interface} {interface} RandomAccess Java Provides a List interface for several implementations AbstractList {interface} {abstract} ArrayList Vector AbstractSequentialList {abstract} Stack LinkedList IT 179

  21. ArrayList LinkedList Vector Stack XXXX List .... .... Interface for the user void add(T item) T insert(int at, T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString() 7 2 6 1 4 3 5 8 IT 179

  22. It’s a generic type, <T> is a type parameter API ArrayList<T> Application Programming Interface ArrayList void add(T item) T insert(int at, T item) void append(T item) T get(int at) T set(int at, T item) int indexOf(T item) T remove(int at) int size() Object[] toArray(Object[] a) String toString() 1 [0] [1] 2 [2] 3 [3] 4 [4] 5 [5] 6 [6] 7 [7] 8 [8] [9] IT 179

More Related