1 / 28

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010. Generics and Collections. “Never recreate from your memory. Always imagine new places .” -- Dom Cobb, Inception .

taurus
Download Presentation

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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

  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 apriori how many elements you need to store • Inserting/removing element at/from a particular index is cumbersome

  4. java.util.ArrayList • Like arrays, hold several values (called elements) • Unlike arrays • Size need not be specifiedat 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.

  5. ArrayList Methods

  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 (inti = 0; i < numbers.size(); i++) { number = (Integer)numbers.get(i); // cast is required; why? // auto-unboxing sum += number; }

  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 }

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

  9. Example import java.util.*; ArrayList<Integer> numbers = new ArrayList<Integer>(); 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; }

  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. Limitation of a single-linked list • Can traverse the list only in the forward direction

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

  16. Inserting into a Double-Linked List

  17. Inserting into a Double-Linked List

  18. Removing from a Double-Linked List

  19. The LinkedList<E> Class • Implements a double-linked list

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

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

  22. Other Collections • Queue<E> • first-in/first-out data structure • Set<E> • Allows no duplicates

  23. 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/util/Collections.html

  24. Comparable<T> interface • Only one method: intcompareTo(T o) public class Student implements Comparable<Student>{ 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 }

  25. Comparator<T> interface • intcompare(T o1, T o2) • 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.

  26. Collection Framework Hierarchy

  27. Get more info! • Java docs: Collections • http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Collection.html • Java Tutorial on Collectionshttp://download.oracle.com/docs/cd/E17409_01/javase/tutorial/collections/ • Java docs: Genericshttp://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/guide/language/generics.html

More Related