EECS 2011 M: Fundamentals of Data Structures

Similar documents
Lecture 4. The Java Collections Framework

The Java Collections Framework. Chapters 7.5

CISC 3115 TY3. C24a: Lists. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/15/2018 CUNY Brooklyn College

Iterators and Sequences

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

What is the Java Collections Framework?

Lists ADT and Iterators

CH7. LIST AND ITERATOR ADTS

INTRODUCTION TO DATA AND PROCEDURE

The Java Collections Framework and Lists in Java Parts 1 & 2

The java.util.list ADT

CSED233: Data Structures (2017F) Lecture7: Lists and Iterators

Arrays. myints = new int[15];

CS Ananda Gunawardena

Computer Science II (Spring )

Frameworks. CS151 Chris Pollett Oct. 26, 2005.

References and Homework ABSTRACT DATA TYPES; LISTS & TREES. Abstract Data Type (ADT) 9/24/14. ADT example: Set (bunch of different values)

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Building Java Programs

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

9/16/2010 CS Ananda Gunawardena

Java Collection Framework

Arrays. Array Definition

Data Structures and Algorithms

Outline. iterator review iterator implementation the Java foreach statement testing

ArrayList. Introduction. java.util.arraylist

CS S-05 Abstract Data Types and Lists 1

Java Collections Framework: Interfaces

Linked List. ape hen dog cat fox. tail. head. count 5

List... NOTE We use List as illustrative example of collections Study the others from textbook!

CONTAİNERS COLLECTİONS

List ADT. B/W Confirming Pages

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Class 32: The Java Collections Framework

CH7. LIST AND ITERATOR ADTS

Algorithms. Produced by. Eamonn de Leastar

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

CS61B Lecture #17. Last modified: Mon Oct 1 13:40: CS61B: Lecture #17 1

STUDENT LESSON A15 ArrayList

Generic classes & the Java Collections Framework. *Really* Reusable Code

COMP6700/2140 Abstract Data Types: Lists and Iteration

Arrays. CSE 142, Summer 2002 Computer Programming 1.

COURSE 4 PROGRAMMING III OOP. JAVA LANGUAGE

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

Lecture 6 Collections

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

Collections and Maps

An Introduction to Data Structures

Data Structure: Lists and Iterators. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Need to access each element of a list. Use index to access an element of the list. It s quadratic, while:

COMP 250. Lecture 8. stack. Sept. 25, 2017

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

Programming II (CS300)

JAVA COLLECTION FRAMEWORK & SETS

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

Class 26: Linked Lists

Generics Collection Framework

ECE 122. Engineering Problem Solving with Java

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

Introduction to Programming Using Java (98-388)

Iterators. Chapter 15. Copyright 2012 by Pearson Education, Inc. All rights reserved

Lists. The List ADT. Reading: Textbook Sections

QUEEN MARY, UNIVERSITY OF LONDON DCS128 ALGORITHMS AND DATA STRUCTURES Class Test Monday 13 th February

Lecture 6: ArrayList Implementation

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

Object-Oriented Programming in the Java language

Lecture 9: Lists. MIT-AITI Kenya 2005

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

COMP 401 Fall Recitation 7: Factories and Lists

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

+ Abstract Data Types

Active Learning: Streams

Implementing a List in Java. CSC 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List CSC

Lecture 18 Tao Wang 1

CS Introduction to Data Structures Week 1 Thursday

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Tha Java Programming Language

Today: Java Library Classes for lists. Iterators, ListIterators. CS61B Lecture #7. Last modified: Fri Sep 12 14:41: CS61B: Lecture #7 1

Lecture Notes Chapter #9 Summery. Inheritance

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

Collections and Iterators. Collections

Pieter van den Hombergh Richard van den Ham. February 8, 2018

CSIS 10B Lab 2 Bags and Stacks

CS 221 Review. Mason Vail

Student Performance Q&A:

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CS/CE 2336 Computer Science II

COMP More About Arrays. Yi Hong June 05, 2015

Basic Data Structures

Transcription:

EECS 2011 M: Fundamentals of Data Structures Suprakash Datta Office: LAS 3043 Course page: http://www.eecs.yorku.ca/course/2011m Also on Moodle S. Datta (York Univ.) EECS 2011 W18 1 / 19

Iterators and the Java Collections Framework Relevant Sections Iterators (Ch. 7.4) The Java Collections Framework (Ch. 7.5) Note: Some slides in this presentation have been adapted from the authors slides. S. Datta (York Univ.) EECS 2011 W18 2 / 19

Iterators Ch 7.4: Iterators in Java enables us to traverse through a collection and to remove elements from the collection selectively created by calling the collection s iterator method. Suppose collection is an instance of a Collection. Then to print out each element on a separate line: I t e r a t o r <E> i t = c o l l e c t i o n. i t e r a t o r ( ) ; while ( i t. hasnext ( ) ) System. out. p r i n t l n ( i t. next ( ) ) ; Note that next() does two things: Returns the current element (initially the first element) Steps to the next element and makes it the current element. S. Datta (York Univ.) EECS 2011 W18 3 / 19

Iterators Iterable Interface Java defines a parameterized interface, named Iterable, that includes the following single method: iterator(): Returns an iterator of the elements in the collection. An instance of a typical collection class in Java, such as an ArrayList, is iterable (but not itself an iterator); it produces an iterator for its collection as the return value of the iterator() method. Each call to iterator() returns a new iterator instance, and allows multiple (even simultaneous) traversals of a collection. S. Datta (York Univ.) EECS 2011 W18 4 / 19

Iterators Iterator Interface p u b l i c i n t e r f a c e I t e r a t o r <E> { boolean hasnext ( ) ; E next ( ) ; void remove ( ) ; // o p t i o n a l } hasnext(): returns true if there are more elements next(): returns the next element; throws exception if there are no more remove(): removes the last element that was returned by next remove may be called only once per call to next otherwise throws an exception Iterator.remove is the only safe way to modify a collection during iteration S. Datta (York Univ.) EECS 2011 W18 5 / 19

for-each Loop Iterators Java s Iterable class enables the for-each loop syntax: f o r ( Xtype o : c o l l e c t i o n ) System. out. p r i n t l n ( o ) ; } prints each element of the collection on a new line. This code is just shorthand: it compiles to use o.iterator() I t e r a t o r <Xtype> i t e r = c o l l e c t i o n. i t e r a t o r ( ) ; while ( i t e r. hasnext ( ) ) { Xtype x = i t e r. next ( ) ; System. out. p r i n t l n ( x ) ; } S. Datta (York Univ.) EECS 2011 W18 6 / 19

More on Iterators Iterators Could represent a sequence, set or map Could be implemented using arrays or linked lists. ListIterator Extends Iterator access to the integer position (index) of elements forward and backward traversal modification and insertion of elements supports the following methods: add(e), hasnext(), hasprevious(), previous(), next(), nextindex(), previousindex(), set(e), remove() S. Datta (York Univ.) EECS 2011 W18 7 / 19

Ch 7.5: The Java Collections Framework good example of how to apply the principles of object-oriented software engineering to the design of classical data structures. A coupled set of classes and interfaces that implement commonly reusable collection data structures Collection: An object (or container) that groups multiple elements into a single unit. S. Datta (York Univ.) EECS 2011 W18 8 / 19

Collections Framework A unified architecture for representing and manipulating collections. Includes: Interfaces: A hierarchy of ADTs. Implementations Algorithms: on objects that implement collection interfaces; these are polymorphic (the same method can be used on many different implementations of the appropriate collection interface) S. Datta (York Univ.) EECS 2011 W18 9 / 19

Java Collections Framework In: Package java.util More details in Javadoc, provided with your java distribution. Comments and code in the specific java.util.*.java files The Collections Java tutorial, available at http://docs.oracle.com/javase/tutorial/collections/inde Chan et al, The Java Class Libraries, Second Edition S. Datta (York Univ.) EECS 2011 W18 10 / 19

Java Collections Framework - 2 In: Package java.util More details in Javadoc, provided with your java distribution. Comments and code in the specific java.util.*.java files The Collections Java tutorial, available at http://docs.oracle.com/javase/tutorial/collections/index.html Chan et al, The Java Class Libraries, Second Edition S. Datta (York Univ.) EECS 2011 W18 11 / 19

Java Collections Framework - 3 Java supports three levels of abstraction Interface Java expression of an ADT Includes method declarations with arguments of specified types, but with empty bodies Abstract Class Implements only a subset of an interface Cannot be used to instantiate an object Class May extend one or more abstract classes Must fully implement any interface it implements Can be used to instantiate objects S. Datta (York Univ.) EECS 2011 W18 12 / 19

Java Collections Framework - 4 From: http://p3lang.com/2013/06/java-collections-framework/ S. Datta (York Univ.) EECS 2011 W18 13 / 19

The iterable interface Allows an Iterator to be associated with an object. The iterator allows an existing data structure to be stepped through sequentially, using the following methods: hasnext() returns true if the iteration has more elements next() returns the next element in the iteration throws exception if iterator has already visited all elements remove() removes the last element that was returned by next remove may be called only once per call to next otherwise throws an exception. Iterator.remove is the only safe way to modify a collection during iteration S. Datta (York Univ.) EECS 2011 W18 14 / 19

The Collection interface Allows data to be modeled as a collection of objects. In addition to the Iterator interface, provides interfaces for: Creating the data structure: add(e), addall(c) Querying the data structure: size(), isempty(), contains(e), containsall(c), toarray(), equals(c) Modifying the data structure: remove(e), removeall(c), retainall(c), clear() S. Datta (York Univ.) EECS 2011 W18 15 / 19

The Abstract Collection class Allows data to be modeled as a collection of objects. In addition to the Iterator interface, provides interfaces for: Skeletal implementation of the Collection interface For unmodifiable collection, programmer still needs to implement: iterator (including hasnext and next methods), size For modifiable collection, need to also implement: remove method for iterator, add S. Datta (York Univ.) EECS 2011 W18 16 / 19

The List interface Extends the Collections interface to model the data as an ordered sequence of elements, indexed by a 0-based integer index (position). Provides interface for creation of a ListIterator Several other interfaces Creating the data structure: add(e): append element e to the list add(i, e): insert element e at position i (and shift elements at i and above one to the right). S. Datta (York Univ.) EECS 2011 W18 17 / 19

The List interface - 2 Querying the data structure get(i): return element currently in position i indexof(e): return index of first occurrence of e lastindexof(e): return index of last occurrence of e sublist(i1, i2): return list of elements from i1 to i2 Modifying the data structure set(i, e) replace element currently stored at index i with specified element e remove(e) remove the first occurrence of the specified element from the list remove(i) remove the element at position i S. Datta (York Univ.) EECS 2011 W18 18 / 19

The Abstract List class Skeletal implementation of the List interface For unmodifiable list, programmer still needs to implement: get, size For modifiable collection, need to also implement: set For variable-size modifiable list, need to implement: add, remove S. Datta (York Univ.) EECS 2011 W18 19 / 19