COP 3503 FALL 2012 Shayan Javed Lecture 8
description
Transcript of COP 3503 FALL 2012 Shayan Javed Lecture 8
![Page 1: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/1.jpg)
1 / 831
COP 3503 FALL 2012SHAYAN JAVED
LECTURE 8
Programming Fundamentals using Java
![Page 2: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/2.jpg)
2 / 83
ArrayList and Java Generics
![Page 3: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/3.jpg)
3 / 83
Collection
A container object that groups multiple objects
![Page 4: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/4.jpg)
4 / 83
Collection
A container object that groups multiple objects Example: Arrays
![Page 5: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/5.jpg)
5 / 83
Collection
A container object that groups multiple objects Example: Arrays
Java provides multiple classes/interfaces in the Java Collection Framework
![Page 6: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/6.jpg)
6 / 83
Collection
A container object that groups multiple objects Example: Arrays
Java provides multiple classes/interfaces in the Java Collection Framework Going to look at ArrayList
![Page 7: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/7.jpg)
7 / 83
Arrays
Hold several elements (primitives/objects) of the same type
![Page 8: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/8.jpg)
8 / 83
Arrays
Hold several elements (primitives/objects) of the same type
Size = static. Once set, cannot be changed
![Page 9: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/9.jpg)
9 / 83
Arrays
Advantages: Easy to access elements directly
![Page 10: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/10.jpg)
10 / 83
Arrays
Advantages: Easy to access elements directly Easy to traverse
![Page 11: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/11.jpg)
11 / 83
Arrays
Advantages: Easy to access elements directly Easy to traverse
Disadvantages: Have to decide on size before creating
![Page 12: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/12.jpg)
12 / 83
Arrays
Advantages: Easy to access elements directly Easy to traverse
Disadvantages: Have to decide on size before creating Inserting/removing elements is troublesome
Shifting elements
![Page 13: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/13.jpg)
13 / 83
ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays)
![Page 14: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/14.jpg)
14 / 83
ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays)
Unlike Arrays: Size does not need to be specified at creation time
![Page 15: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/15.jpg)
15 / 83
ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays)
Unlike Arrays: Size does not need to be specified at creation time Grows in size as items added
![Page 16: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/16.jpg)
16 / 83
ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays)
Unlike Arrays: Size does not need to be specified at creation time Grows in size as items added
Provides useful methods for manipulating the list
![Page 17: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/17.jpg)
17 / 83
ArrayList (java.util.ArrayList)
Constructors: new ArrayList( ) - no need to pass in size (10)
![Page 18: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/18.jpg)
18 / 83
ArrayList (java.util.ArrayList)
Constructors: new ArrayList( ) - no need to pass in size (10) new ArrayList(int) - initial capacity
![Page 19: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/19.jpg)
19 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list
![Page 20: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/20.jpg)
20 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index
![Page 21: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/21.jpg)
21 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements
![Page 22: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/22.jpg)
22 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list
![Page 23: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/23.jpg)
23 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index
![Page 24: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/24.jpg)
24 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list
![Page 25: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/25.jpg)
25 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list Object remove(int index): Removes the object at the index
![Page 26: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/26.jpg)
26 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list Object remove(int index): Removes the object at the index int size(): Returns the no. of elements in
the list
![Page 27: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/27.jpg)
27 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list Object remove(int index): Removes the object at the index int size(): Returns the no. of elements in
the list int indexOf(Object o): Returns index of first matching element
![Page 28: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/28.jpg)
28 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list Object remove(int index): Removes the object at the index int size(): Returns the no. of elements in
the list int indexOf(Object o): Returns index of first matching element Object set(int index, Object o): Sets the object at the index
![Page 29: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/29.jpg)
29 / 83
ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list void add(int index, Object o): Inserts object at specified index void clear(): Removes all elements boolean contains(Object o): Returns true if object is in the list Object get(int index): Returns object at the index boolean remove(Object o): Removes the object from the list Object remove(int index): Removes the object at the index int size(): Returns the no. of elements in
the list int indexOf(Object o): Returns index of first matching element Object set(int index, Object o): Sets the object at the index Object clone(): Returns a shallow copy
![Page 30: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/30.jpg)
30 / 83
ArrayList Example
import java.util.ArrayList;
![Page 31: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/31.jpg)
31 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
![Page 32: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/32.jpg)
32 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));
![Page 33: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/33.jpg)
33 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */
![Page 34: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/34.jpg)
34 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */
int sum = 0; // sum up all the numbers in the list
int number = 0;
![Page 35: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/35.jpg)
35 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */
int sum = 0; // sum up all the numbers in the list
int number = 0;for(int i = 0; i < numbers.size(); i++) {
}
![Page 36: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/36.jpg)
36 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */
int sum = 0; // sum up all the numbers in the list
int number = 0;for(int i = 0; i < numbers.size(); i++) {
number = (Integer)numbers.get(i); // cast required – why?
}
![Page 37: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/37.jpg)
37 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */
int sum = 0; // sum up all the numbers in the list
int number = 0;for(int i = 0; i < numbers.size(); i++) {
number = (Integer)numbers.get(i); // cast required – why?sum += number; // auto un-boxing: Integer converted to int
}
![Page 38: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/38.jpg)
38 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */numbers.add(“A String”); // will compile!int sum = 0; // sum up all the numbers in
the listint number = 0;for(int i = 0; i < numbers.size(); i++) {
number = (Integer)numbers.get(i); // cast required – why?sum += number; // auto un-boxing: Integer converted to int
}
![Page 39: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/39.jpg)
39 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList numbers = new ArrayList(); // no size specified initially
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: converted to numbers.add(new
Integer(3)); */numbers.add(“A String”); // will compile!int sum = 0; // sum up all the numbers in
the listint number = 0;for(int i = 0; i < numbers.size(); i++) {
number = (Integer)numbers.get(i); // run-time error when i = 2sum += number; // auto un-boxing: Integer converted to int
}
![Page 40: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/40.jpg)
40 / 83
ArrayList
How do we make sure we are dealing withobjects of only one type?
![Page 41: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/41.jpg)
41 / 83
ArrayList
How do we make sure we are dealing withobjects of only one type?
Solution: Java Generics
![Page 42: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/42.jpg)
42 / 83
Java Generics
Generics is the capability to parameterize types
![Page 43: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/43.jpg)
43 / 83
Java Generics
Generics is the capability to parameterize types
Can define class/interface/method with generic types – compiler replaces with concrete types.
![Page 44: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/44.jpg)
44 / 83
Java Generics
Generics is the capability to parameterize types
Can define class/interface/method with generic types – compiler replaces with concrete types.
Introduced in JDK 1.5
![Page 45: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/45.jpg)
45 / 83
java.util.ArrayList<E>
Methods: boolean add(E o): Appends object at the end of the
list void add(int index, E o): Inserts object at specified index void clear(): Removes all elements boolean contains(E o): Returns true if object is in the list E get(int index): Returns object at the index boolean remove(E o): Removes the object from the list E remove(int index): Removes the object at the index int size(): Returns the no. of elements in
the list int indexOf(E o): Returns index of first matching element E set(int index, E o): Sets the object at the index E clone(): Returns a shallow copy
![Page 46: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/46.jpg)
46 / 83
Java Generics
Type of objects it can hold is specified at declaration
![Page 47: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/47.jpg)
47 / 83
Java Generics
Type of objects it can hold is specified at declaration
// will only hold Integer objectsArrayList<Integer> numbers = new
ArrayList<Integer>();
![Page 48: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/48.jpg)
48 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: */numbers.add(“A String”); // WON’T COMPILE!
int sum = 0; // sum up all the numbers in the list
int number = 0;for(int i = 0; i < numbers.size(); i++) {
number = (Integer)numbers.get(i); // cast NOT required – why?sum += number; // auto un-boxing: Integer converted to int
}
![Page 49: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/49.jpg)
49 / 83
ArrayList Example
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(new Integer(10));numbers.add(3); /* auto-boxing: */numbers.add(“A String”); // WON’T COMPILE!
int sum = 0; // sum up all the numbers in the list
int number = 0;for(int i = 0; i < numbers.size(); i++) {
number = numbers.get(i); // cast NOT required – why?sum += number; // auto un-boxing: Integer converted to int
}
![Page 50: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/50.jpg)
50 / 83
Java Generics
More strictness
![Page 51: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/51.jpg)
51 / 83
Java Generics
More strictness Fewer errors
![Page 52: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/52.jpg)
52 / 83
Generic Classes
public class AClass<E> {// properties and methods can use the “E” type
}
![Page 53: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/53.jpg)
53 / 83
Generic Classes
public class AClass<E> {// properties and methods can use the “E” typeArrayList<E> numbers = new ArrayList<E>();
}
![Page 54: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/54.jpg)
54 / 83
Generic Methods
public class GenericMethodDemo { public static void main(String[] args ) {
Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York”};
}
![Page 55: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/55.jpg)
55 / 83
Generic Methods
public class GenericMethodDemo { public static void main(String[] args ) {
Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York”};
}
public static <E> void print(E[] list) { for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " "); System.out.println(); }
}
![Page 56: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/56.jpg)
56 / 83
Generic Methods
public class GenericMethodDemo { public static void main(String[] args ) {
Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York”};
GenericMethodDemo.<Integer>print(integers); GenericMethodDemo.<String>print(strings);
}
public static <E> void print(E[] list) { for (int i = 0; i < list.length; i++)
System.out.print(list[i] + " "); System.out.println(); }
}
![Page 57: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/57.jpg)
57 / 83
Generic Interfaces
public interface AnInterface<E> {// properties and methods can use the “E” type
public void aMethod(E object);}
![Page 58: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/58.jpg)
58 / 83
Generic Interfaces
public interface Comparable<E> {
public int compareTo(E object);
}
![Page 59: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/59.jpg)
59 / 83
Generic Interfaces
public interface Comparable<E> {
public int compareTo(E object);
}
public interface Comparator<E> {
public int compare(E object1, E object2);
}
![Page 60: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/60.jpg)
60 / 83
Generic Interfaces
public Book implements Comparable{
// comparison based on pricepublic int compareTo(Object o) {Book b = (Book)o;if (price > b.getPrice())return 1;else if (price < b.getPrice())return -1;elsereturn 0;}
}
![Page 61: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/61.jpg)
61 / 83
Generic Interfaces
public Book implements Comparable{
// comparison based on pricepublic int compareTo(Object o) {Book b = (Book)o;if (price > b.getPrice())return 1;else if (price < b.getPrice())return -1;elsereturn 0;}
} HOW WOULD YOU USE JAVA GENERICS?
![Page 62: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/62.jpg)
62 / 83
Generic Interfaces
public Book implements Comparable<Book>{
// comparison based on pricepublic int compareTo(Book b) {Book b = (Book)o;if (price > b.getPrice())return 1;else if (price < b.getPrice())return -1;elsereturn 0;}
}
![Page 63: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/63.jpg)
63 / 83
Generic Interfaces
public Book implements Comparable<Book>{
// comparison based on pricepublic int compareTo(Book b) {Book b = (Book)o; // no more casting!if (price > b.getPrice())return 1;else if (price < b.getPrice())return -1;elsereturn 0;}
}
![Page 64: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/64.jpg)
64 / 83
Generic Interfaces
public Book implements Comparable<Book>{
// comparison based on pricepublic int compareTo(Book b) {Book b = (Book)o; // no more casting!if (price > b.getPrice())return 1;else if (price < b.getPrice())return -1;elsereturn 0;}
}
![Page 65: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/65.jpg)
65 / 83
Generic Interfaces
public class ReviewComparison implements Comparator {public int compare (Object o1, Object o2) {
Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getACR() > b2.getACR())
return 1;if (b1.getACR() < b2.getACR())
return -1;else
return 0;}
}
![Page 66: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/66.jpg)
66 / 83
Generic Interfaces
public class ReviewComparison implements Comparator<Book> {public int compare (Book b1, Book b2) {Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getACR() > b2.getACR())return 1;if (b1.getACR() < b2.getACR())return -1;elsereturn 0;}
}
![Page 67: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/67.jpg)
67 / 83
Generic Interfaces
public class ReviewComparison implements Comparator<Book> {public int compare (Book b1, Book b2) {Book b1 = (Book)o1;Book b2 = (Book)o2;if (b1.getACR() > b2.getACR())return 1;if (b1.getACR() < b2.getACR())return -1;elsereturn 0;}
}
![Page 68: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/68.jpg)
68 / 83
Java Generics - Summary
Allows you to create a “general” template
Classes/methods/interfaces
Versatile and strict
![Page 69: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/69.jpg)
69 / 83
Back to ArrayList...
How does it work “behind the scenes”?
![Page 70: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/70.jpg)
70 / 83
Back to ArrayList...
How does it work “behind the scenes”?
Elements are stored inside an array private array
![Page 71: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/71.jpg)
71 / 83
Back to ArrayList...
How does it work “behind the scenes”?
Elements are stored inside an array private array
Array has an initial capacity
![Page 72: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/72.jpg)
72 / 83
Back to ArrayList...
How does it work “behind the scenes”?
Elements are stored inside an array private array
Array has an initial capacity Empty constructor [new ArrayList()]: capacity 10
![Page 73: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/73.jpg)
73 / 83
Back to ArrayList...
How does it work “behind the scenes”?
Elements are stored inside an array private array
Array has an initial capacity Empty constructor [new ArrayList()]: capacity 10 Otherwise specify capacity [new ArrayList(capacity)]
![Page 74: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/74.jpg)
74 / 83
ArrayList
When number of elements exceeds capacity (the add/insert method):
![Page 75: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/75.jpg)
75 / 83
ArrayList
When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one
![Page 76: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/76.jpg)
76 / 83
ArrayList
When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one Elements from the old array copied over
![Page 77: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/77.jpg)
77 / 83
ArrayList
When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one Elements from the old array copied over
Insert and remove require shifting of elements
![Page 78: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/78.jpg)
78 / 83
ArrayList
When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one Elements from the old array copied over
Insert and remove require shifting of elements (Recall add/remove in Project1)
![Page 79: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/79.jpg)
79 / 83
Efficiency of ArrayLists
Reallocation of underlying array = when capacity reached and need to add more elements
![Page 80: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/80.jpg)
80 / 83
Efficiency of ArrayLists
Reallocation of underlying array = when capacity reached and need to add more elements
Add/Insert/Remove = costly because requires shifting of elements in array
![Page 81: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/81.jpg)
81 / 83
Efficiency of ArrayLists
Reallocation of underlying array = when capacity reached and need to add more elements
Add/Insert/Remove = costly because requires shifting of elements in array
Accessing element (get) = fast just like an array
![Page 82: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/82.jpg)
82 / 83
Efficiency of ArrayLists
Reallocation of underlying array = when capacity reached and need to add more elements
Add/Insert/Remove = costly because requires shifting of elements in array
Accessing element (get) = fast just like an array
Problems overcome with Linked Lists.
![Page 83: COP 3503 FALL 2012 Shayan Javed Lecture 8](https://reader035.fdocuments.net/reader035/viewer/2022062811/568161af550346895dd170a8/html5/thumbnails/83.jpg)
83 / 83
Other Java Collections
LinkedList
Stack
Queue
Will look at these later