Presentation is loading. Please wait.

Presentation is loading. Please wait.

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.

Similar presentations


Presentation on theme: "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never."— Presentation transcript:

1 CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never recreate from your memory. Always imagine new places.” -- Dom Cobb, Inception

2 Collection A container object that groups multiple objects into a single unit Example: Arrays Related Classes/Interfaces together form the Java Collection Framework Part of java.util package 2

3 Recall: Arrays Hold several objects (called elements) of the same type Static in size– once their size is set, it can not be changed (expanded/shrunk) Issues: you have to decide a priori how many elements you need to store Inserting/removing element at/from a particular index is cumbersome 3

4 java.util.ArrayList Like arrays, hold several values (called elements) Unlike arrays Size need not be specified at the time that the collection object is first created Will automatically grow in size as new items are added (true of virtually all collections besides arrays) Provides methods to perform operations that arrays don’t provide. 4

5 ArrayList Methods 5

6 ArrayList Example import java.util.ArrayList; ArrayList numbers = new ArrayList(); // no type or size specified //designed to hold objects of type Object numbers.add(new Integer(7)); numbers.add(3); //auto-boxing; same as numbers.add(new Integer(3)); int sum = 0; int number = 0; for (int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast is required; why? // auto-unboxing sum += number; } 6

7 ArrayList Example import java.util.*; ArrayList numbers = new ArrayList(); numbers.add(3); numbers.add(7); numbers.add(“Test”); // will compile int sum = 0; int number = 0; for (int i = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); sum += number; // runtime error when i= 2 } 7

8 Generic ArrayList (introduced in Java 5) The type of objects it can hold is specified at the time of declaration ArrayList numbers = new ArrayList (); 8

9 Example import java.util.*; ArrayList numbers = new ArrayList (); numbers.add(3); numbers.add(7); numbers.add("Test"); // won't compile – prevents runtime error int sum = 0; int number = 0; for (int i = 0; i < numbers.size(); i++) { number = numbers.get(i) // no cast needed sum += number; } 9

10 10 Placeholder for a “real” type

11 ArrayList Implementation in Java Collections Framework The elements of the ArrayList are stored in an array This array is a private member This array has an initial capacity Which is either specified in constructor Otherwise is set to a default value When the number of elements in the list exceeds the capacity, the internal array is replaced by a bigger one

12 Efficiency of ArrayLists ArrayList Reallocation of underlying array required when the array is full and you need to add more elements Add and remove operations are costly because they require shifting of elements in the underlying array => Linked lists overcomes this

13 Singly-linked list Consists of a number of nodes chained together Each node in a linked list stores Data (or element) Link (or reference) to the next node

14

15 Limitation of a single-linked list Can traverse the list only in the forward direction

16 Doubly-linked lists Removes the limitation of singly-linked lists by adding a reference in each node to the previous node DoublyLinkedList head tail

17 Inserting into a Double-Linked List

18

19 Removing from a Double-Linked List

20 The LinkedList Class Implements a double-linked list

21 Stack Represents something like a stack of books on a table Last-in-first-out structure You can add, or remove an element only from the top of the stack. Main Operations: push pop peek

22 java.util.Stack JAVA API does not implement a “pure” stack Extends Vector which is like an ArrayList Hence allows access of elements at any index

23 Other Collections Queue first-in/first-out data structure Set Allows no duplicates

24 The Collections Class Consists of static methods that operate on or return collections Some useful methods: sort, binarySearch, min, max, reverse For details: see http://java.sun.com/javase/6/docs/api/java/ut il/Collections.html http://java.sun.com/javase/6/docs/api/java/ut il/Collections.html

25 Comparable interface Only one method: int compareTo(T o) public class Student implements Comparable { String id; String name;double gpa; public int compareTo(Student s) { return this.name.compareTo(s.name); } main(…) { Student s1 = new Student(); Student s2 = new Student(); System.out.println(s1.compareTo(s2)); //ok System.out.println(s1.compareTo(“aaa”)); // compiler error }

26 Comparator interface int compare(T o1, T o2)T Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

27 Collection Framework Hierarchy

28 Get more info! Java docs: Collections http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/ut il/Collection.html Java Tutorial on Collections http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/collections/ http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/collections/ Java docs: Generics http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/lang uage/generics.html http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/lang uage/generics.html


Download ppt "CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never."

Similar presentations


Ads by Google