+ All Categories
Home > Documents > COP 3503 FALL 2012 Shayan Javed Lecture 8

COP 3503 FALL 2012 Shayan Javed Lecture 8

Date post: 22-Feb-2016
Category:
Upload: afya
View: 33 times
Download: 0 times
Share this document with a friend
Description:
COP 3503 FALL 2012 Shayan Javed Lecture 8. Programming Fundamentals using Java. ArrayList and Java Generics. Collection. A container object that groups multiple objects. Collection. A container object that groups multiple objects Example: Arrays. Collection. - PowerPoint PPT Presentation
Popular Tags:
83
1 / 83 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 8 Programming Fundamentals using Java 1
Transcript
Page 1: COP 3503  FALL 2012 Shayan Javed Lecture 8

1 / 831

COP 3503 FALL 2012SHAYAN JAVED

LECTURE 8

Programming Fundamentals using Java

Page 2: COP 3503  FALL 2012 Shayan Javed Lecture 8

2 / 83

ArrayList and Java Generics

Page 3: COP 3503  FALL 2012 Shayan Javed Lecture 8

3 / 83

Collection

A container object that groups multiple objects

Page 4: COP 3503  FALL 2012 Shayan Javed Lecture 8

4 / 83

Collection

A container object that groups multiple objects Example: Arrays

Page 5: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

7 / 83

Arrays

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

Page 8: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

9 / 83

Arrays

Advantages: Easy to access elements directly

Page 10: COP 3503  FALL 2012 Shayan Javed Lecture 8

10 / 83

Arrays

Advantages: Easy to access elements directly Easy to traverse

Page 11: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

13 / 83

ArrayList (java.util.ArrayList)

Holds several objects in order (like Arrays)

Page 14: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

30 / 83

ArrayList Example

import java.util.ArrayList;

Page 31: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

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

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

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

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

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

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

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

42 / 83

Java Generics

Generics is the capability to parameterize types

Page 43: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

46 / 83

Java Generics

Type of objects it can hold is specified at declaration

Page 47: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

50 / 83

Java Generics

More strictness

Page 51: COP 3503  FALL 2012 Shayan Javed Lecture 8

51 / 83

Java Generics

More strictness Fewer errors

Page 52: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

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

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

58 / 83

Generic Interfaces

public interface Comparable<E> {

public int compareTo(E object);

}

Page 59: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

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

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

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

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

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

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

69 / 83

Back to ArrayList...

How does it work “behind the scenes”?

Page 70: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

74 / 83

ArrayList

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

Page 75: COP 3503  FALL 2012 Shayan Javed Lecture 8

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

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

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

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

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

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

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

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

83 / 83

Other Java Collections

LinkedList

Stack

Queue

Will look at these later


Recommended