220 likes | 585 Views
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}.
E N D
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
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
Tow kinds of Lists Array Linked List IT 179
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
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
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
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
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
Resizing – O(size of new array)operation 4 8 IT 179
Inserting an element – O(n) Deleting an element – O(n) Delete 7 6 6 ? Resizing operation is needed IT 179
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
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
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 ITK 179 19 10/8/2014 IT 179
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
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
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