Presentation is loading. Please wait.

Presentation is loading. Please wait.

COP 3503 FALL 2012 Shayan Javed Lecture 8

Similar presentations


Presentation on theme: "COP 3503 FALL 2012 Shayan Javed Lecture 8"— Presentation transcript:

1 COP 3503 FALL 2012 Shayan Javed Lecture 8
Programming Fundamentals using Java

2 ArrayList and Java Generics

3 Collection A container object that groups multiple objects

4 Collection A container object that groups multiple objects
Example: Arrays

5 Collection A container object that groups multiple objects
Example: Arrays Java provides multiple classes/interfaces in the Java Collection Framework

6 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

7 Arrays Hold several elements (primitives/objects) of the same type

8 Arrays Hold several elements (primitives/objects) of the same type
Size = static. Once set, cannot be changed

9 Arrays Advantages: Easy to access elements directly

10 Arrays Advantages: Easy to access elements directly Easy to traverse

11 Arrays Advantages: Disadvantages: Easy to access elements directly
Easy to traverse Disadvantages: Have to decide on size before creating

12 Arrays Advantages: Disadvantages: Easy to access elements directly
Easy to traverse Disadvantages: Have to decide on size before creating Inserting/removing elements is troublesome Shifting elements

13 ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays)

14 ArrayList (java.util.ArrayList)
Holds several objects in order (like Arrays) Unlike Arrays: Size does not need to be specified at creation time

15 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

16 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

17 ArrayList (java.util.ArrayList)
Constructors: new ArrayList( ) - no need to pass in size (10)

18 ArrayList (java.util.ArrayList)
Constructors: new ArrayList( ) - no need to pass in size (10) new ArrayList(int) - initial capacity

19 ArrayList (java.util.ArrayList)
Methods: boolean add(Object o): Appends object at the end of the list

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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

28 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

29 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

30 ArrayList Example import java.util.ArrayList;

31 ArrayList Example import java.util.ArrayList; ArrayList numbers = new ArrayList(); // no size specified initially

32 ArrayList Example import java.util.ArrayList; ArrayList numbers = new ArrayList(); // no size specified initially numbers.add(new Integer(10));

33 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)); */

34 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;

35 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++) { }

36 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? }

37 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 }

38 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 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 }

39 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 list int number = 0; for(int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // run-time error when i = 2 sum += number; // auto un-boxing: Integer converted to int }

40 How do we make sure we are dealing with objects of only one type?
ArrayList How do we make sure we are dealing with objects of only one type?

41 ArrayList How do we make sure we are dealing with objects of only one type? Solution: Java Generics

42 Java Generics Generics is the capability to parameterize types

43 Java Generics Generics is the capability to parameterize types
Can define class/interface/method with generic types – compiler replaces with concrete types.

44 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

45 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

46 Java Generics Type of objects it can hold is specified at declaration

47 Java Generics Type of objects it can hold is specified at declaration
// will only hold Integer objects ArrayList<Integer> numbers = new ArrayList<Integer>();

48 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 }

49 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 }

50 Java Generics More strictness

51 Java Generics More strictness Fewer errors

52 Generic Classes public class AClass<E> { // properties and methods can use the “E” type }

53 Generic Classes public class AClass<E> { // properties and methods can use the “E” type ArrayList<E> numbers = new ArrayList<E>(); }

54 Generic Methods public class GenericMethodDemo { public static void main(String[] args ) { Integer[] integers = {1, 2, 3, 4, 5}; String[] strings = {"London", "Paris", "New York”}; }

55 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(); }

56 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(); }

57 Generic Interfaces public interface AnInterface<E> { // properties and methods can use the “E” type public void aMethod(E object); }

58 Generic Interfaces public interface Comparable<E> { public int compareTo(E object); }

59 Generic Interfaces public interface Comparable<E> { public int compareTo(E object); } public interface Comparator<E> { public int compare(E object1, E object2);

60 Generic Interfaces public Book implements Comparable{ // comparison based on price public int compareTo(Object o) { Book b = (Book)o; if (price > b.getPrice()) return 1; else if (price < b.getPrice()) return -1; else return 0; }

61 Generic Interfaces public Book implements Comparable{ // comparison based on price public int compareTo(Object o) { Book b = (Book)o; if (price > b.getPrice()) return 1; else if (price < b.getPrice()) return -1; else return 0; } } HOW WOULD YOU USE JAVA GENERICS?

62 Generic Interfaces public Book implements Comparable<Book>{ // comparison based on price public int compareTo(Book b) { Book b = (Book)o; if (price > b.getPrice()) return 1; else if (price < b.getPrice()) return -1; else return 0; }

63 Generic Interfaces public Book implements Comparable<Book>{ // comparison based on price public int compareTo(Book b) { Book b = (Book)o; // no more casting! if (price > b.getPrice()) return 1; else if (price < b.getPrice()) return -1; else return 0; }

64 Generic Interfaces public Book implements Comparable<Book>{ // comparison based on price public int compareTo(Book b) { Book b = (Book)o; // no more casting! if (price > b.getPrice()) return 1; else if (price < b.getPrice()) return -1; else return 0; }

65 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; }

66 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; else return 0; }

67 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; else return 0; }

68 Java Generics - Summary
Allows you to create a “general” template Classes/methods/interfaces Versatile and strict

69 Back to ArrayList... How does it work “behind the scenes”?

70 Back to ArrayList... How does it work “behind the scenes”?
Elements are stored inside an array private array

71 Back to ArrayList... How does it work “behind the scenes”?
Elements are stored inside an array private array Array has an initial capacity

72 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

73 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)]

74 ArrayList When number of elements exceeds capacity (the add/insert method):

75 ArrayList When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one

76 ArrayList When number of elements exceeds capacity (the add/insert method): Internal array replaced by new one Elements from the old array copied over

77 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

78 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)

79 Efficiency of ArrayLists
Reallocation of underlying array = when capacity reached and need to add more elements

80 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

81 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

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

83 Other Java Collections
LinkedList Stack Queue Will look at these later


Download ppt "COP 3503 FALL 2012 Shayan Javed Lecture 8"

Similar presentations


Ads by Google