Java Programming: Guided Learning with Early Objects

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
Data Structures Using C++
John Hurley Cal State LA
The List ADT Textbook Sections
Generics and the ArrayList Class
Data Structures: A Pseudocode Approach with C
Lecture 8 CS203. Implementation of Data Structures 2 In the last couple of weeks, we have covered various data structures that are implemented in the.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Alice in Action with Java
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
C++ Programming: Program Design Including Data Structures, Second Edition Chapter 22: Standard Template Library (STL)
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 19 Java Data Structures
Data Structures Using C++ 2E
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Data Structures Using Java1 Chapter 4 Linked Lists.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Data Structures Using C++1 Chapter 5 Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 16: Searching, Sorting, and the vector Type.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Linked Lists Chapter 5 (continued)
Review Deleting an Element from a Linked List Deletion involves:
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Chapter 20 Generic Classes and Methods
Dynamic Data Structures and Generics
Object Oriented Programming in java
Linked Lists Chapter 5 (continued)
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Presentation transcript:

Java Programming: Guided Learning with Early Objects Chapter 12 Generics, Dynamic Representations, and Collections

Objectives Learn about generic methods and classes Learn about the interface Comparable and how to implement it Learn about the Java class LinkedList and how to use various operations of this class Java Programming: Guided Learning with Early Objects

Objectives (continued) Explore iterators and how they work Learn about the interface Collection and its framework Learn about various collection algorithms Java Programming: Guided Learning with Early Objects

Generic Methods Generic methods: write one method definition rather than multiple definitions Defined using type parameters Type parameter section precedes return type of the method Type parameters separated by commas Enclosed in angle brackets Also called type variables Java Programming: Guided Learning with Early Objects

Generic Methods (continued) Type parameters used for: Declaring return type of the method Declaring formal parameters Declaring local variables Type parameters represent only reference parameters Not primitives modifiers <T> returnType methodName (formal parameter list) { // Method body } Java Programming: Guided Learning with Early Objects

Generic Classes Type parameters are enclosed in angle brackets Placed after the name of the class Type parameter declares variables and generic methods within the class modifiers className<T> modifiers { //class members } Also known as parametric classes Java Programming: Guided Learning with Early Objects

Generic Classes (continued) Reference variables declared using type parameter T within definition of the class Cannot instantiate objects using type parameter Java Programming: Guided Learning with Early Objects

The interface Comparable One method: compareTo Used to require a class to provide definition for the method compareTo Allows values of two objects to be compared Make the class implement interface Comparable Java Programming: Guided Learning with Early Objects

Generic Methods and Bounded Type Parameters Recall generic method print Type parameter T refers to any built-in Java class No restriction on type parameter T Type parameters restricted, for example, to classes that define compareTo Example: public static <T extends Comparable<T>> T larger( T x, T y ) Java Programming: Guided Learning with Early Objects

Generic Methods and Bounded Type Parameters (continued) Consider: <T extends Comparable<T>> T refers to any class that implements Comparable Comparable is the upper bound of the type parameter T By default, Object always an upper bound Keyword extends used when type parameter declaration bounds a parameter Java Programming: Guided Learning with Early Objects

Generic Search and Sort Algorithms (Optional) Sort algorithms from earlier chapters work only on int arrays Designing generic search and sort algorithms Generic selection sort Place definition of selection algorithm in a class Java Programming: Guided Learning with Early Objects

Collections Vector known as a collection class class Vector implements array-based lists class LinkedList implements a linked list Java Programming: Guided Learning with Early Objects

Linked Lists and the class LinkedList Array also called a sequential list Operations on sequential lists: Insertion Deletion Searching Searching unsorted data is time-consuming Insertion and deletion is time-consuming on sorted lists Array size is fixed Java Programming: Guided Learning with Early Objects

Linked Lists Linked list: collection of components called nodes Each node contains address of next node Two parts: One stores relevant information One stores address Address of first node is called head or first class Node defines the node of a linked list Two instance variables: info and link Java Programming: Guided Learning with Early Objects

Figure 12-3 Linked list and values of the links Java Programming: Guided Learning with Early Objects

Linked Lists: Some Properties Keep a reference variable current to point to address of the current node in the list Useful for traversing the list Java Programming: Guided Learning with Early Objects

Figure 12-4 Linked list with four nodes Java Programming: Guided Learning with Early Objects

Table 12-1 Values of head and Some of the Nodes of the Linked List in Figure 12-4 Java Programming: Guided Learning with Early Objects

Figure 12-5 Linked list after the statement current = current Figure 12-5 Linked list after the statement current = current.link; executes Java Programming: Guided Learning with Early Objects

Table 12-2 Values of current and Some of the Nodes of the Linked List in Figure 12-5 Java Programming: Guided Learning with Early Objects

Insertion Create a new node Store data in instance variable info Set instance variable link to point to following node Set previous node variable link to point to new node Java Programming: Guided Learning with Early Objects

Figure 12-6 Linked list before item insertion Java Programming: Guided Learning with Early Objects

Figure 12-7 Create newNode and store 50 in it Java Programming: Guided Learning with Early Objects

Figure 12-8 List after the statement newNode.link = p.link; executes Java Programming: Guided Learning with Early Objects

Figure 12-9 List after the statement p.link = newNode; executes Java Programming: Guided Learning with Early Objects

Deletion Set link variable of previous node to link to next node in list Bypass the node to be deleted Garbage collector runs if no reference variable points to the deleted node Memory reclaimed by system Java Programming: Guided Learning with Early Objects

Figure 12-10 Node to be deleted is with info 34 Java Programming: Guided Learning with Early Objects

Figure 12-11 List after the statement p.link = p.link.link; executes Java Programming: Guided Learning with Early Objects

Doubly Linked Lists Every node has a next pointer and a previous pointer Can be traversed efficiently in either direction Can traverse from first or last node Inserting a node requires setting pointers in both directions Deleting a node requires bypassing a node in both directions Java Programming: Guided Learning with Early Objects

Figure 12-12 Doubly linked list Java Programming: Guided Learning with Early Objects

The class LinkedList class LinkedList implemented as a doubly linked list Every element points to immediate predecessor and successor Two constructors: Collection<? extends E> Type parameter ? represents unknown type ? called wildcard Type E an upper bound of the wildcard Java Programming: Guided Learning with Early Objects

Iterators Process elements of a collection one by one Use an iterator Could use foreach loop Use an iterator Processes elements in collection one after the other interface ListIterator implements interface Iterator interface Iterator has only three methods Java Programming: Guided Learning with Early Objects

Table 12-4 Methods of the interface Iterator Java Programming: Guided Learning with Early Objects

Table 12-5 The interface ListIterator and its Methods Java Programming: Guided Learning with Early Objects

Table 12-5 The interface ListIterator and its Methods (continued) Java Programming: Guided Learning with Early Objects

Iterators (continued) Typically, iterator moves in only one direction Starts at first element, moves to last class LinkedList implemented as doubly linked lists Iterator should move in both directions interface ListInterface extends interface Iterator Has methods to move in both directions Can also be used for insertion and deletion Java Programming: Guided Learning with Early Objects

Collections and Collection Algorithms Collection: data structure that holds references to other objects Examples: vector, array Collection class: a class whose objects are collections Example: class Vector Several interfaces and classes implement collections systematically Hierarchical structure Java Programming: Guided Learning with Early Objects

Figure 12-13 Some Java Collection classes and the inheritance hierarchy Java Programming: Guided Learning with Early Objects

Table 12-6 Methods of the Collections class Java Programming: Guided Learning with Early Objects

The Method addAll Method addAll adds the elements of a collection into another collection List to be added is a variable length parameter Elements specified individually or as an array Throws: UnsupportedOperationException, NullPointerException, IllegalArgumentException Java Programming: Guided Learning with Early Objects

The Methods binarySearch, sort, and reverseOrder binarySearch: search the list, specified as parameter for object Efficiently searches a list for a given element Requires a list to be sorted List can be sorted in ascending or descending order using reverseOrder sort: type parameter T bounded by interface Comparable Java Programming: Guided Learning with Early Objects

Table 12-7 Methods of the interface Comparator Java Programming: Guided Learning with Early Objects

The Method copy Copies elements of one collection into another collection object After copy operation, index of each copied element is identical in source and destination Copies only references of the objects Objects in destination list point to same objects as source Objects in source list are mutable Changing value of object by using source changes value in destination Java Programming: Guided Learning with Early Objects

The Methods max and min Methods max and min determine maximum and minimum elements All elements in collection must be mutually comparable by the comparator Throws exceptions: ClassCastException NoSuchElementException Java Programming: Guided Learning with Early Objects

The Methods fill and frequency Method fill replaces all elements of a given list with given element Type parameter T specifies type of list elements as well as type of replacement element Method frequency finds the number of times a specific object appears in a collection If collection is null, throws NullPointerException Java Programming: Guided Learning with Early Objects

The Method replaceAll Replaces all occurrences of a given element with another Type parameter T specifies type of list elements Returns true if list contains one or more elements equal to replacement object If list or list iterator does not support set method, throws exception Java Programming: Guided Learning with Early Objects

The Methods shuffle and swap Method shuffle randomly shuffles elements of a list Two forms: with and without random seed All permutations occur with approximately equal likelihood Method swap swaps elements of list Java Programming: Guided Learning with Early Objects

Summary Generic methods are defined using type parameters Every generic method has a type parameter section preceding the return type of the method Type parameters separated by commas Enclosed in angle brackets Type parameters specify generic type names Within generic method, declare reference variables using type parameter T Java Programming: Guided Learning with Early Objects

Summary (continued) Type parameter may be bounded or unbounded Cannot instantiate objects using type parameter class LinkedList implemented as a doubly linked list Contained in package java.util The foreach loop iterates over LinkedList Iterator iterates over elements of a collection Java Programming: Guided Learning with Early Objects

Summary (continued) Collection is a data structure that holds references to objects Collection class is a class whose objects are collections interface Collection is the superclass of all Java collection classes Generic algorithms designed as static methods of class Collections Java Programming: Guided Learning with Early Objects