CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
John Hurley Cal State LA
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 4 The Java Collections Framework. Java Container Classes.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
LinkedList Many slides from Horstmann modified by Dr V.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
1 Interfaces in Java’s Collection Framework Rick Mercer.
COMP 121 Week 11: Linked Lists.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
List Interface and Linked List Mrs. Furman March 25, 2010.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
CS-2852 Data Structures LECTURE 5 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
1 Collections. 2 Concept A collection is a data structure – actually, an object – to hold other objects, which let you store and organize objects in useful.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Iterator Summary Slides by Entesar Al-Mosallam adopted from Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
University of Limerick1 Collections The Collection Framework.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
COMP2402 The Java Collections Framework II: Implementations and Algorithms Pat Morin.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
University of Central Florida COP 3330 Object Oriented Programming
Introduction to Collections
Introduction to Collections
Introduction to Collections
Introduction to Collections
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Iterators Dan Fleck.
Part of the Collections Framework
CSC 205 – Java Programming II
Java Generics & Iterators
Presentation transcript:

CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com

CS-2852 Data Structures, Andrew J. Wozniewicz Agenda JCF iterator interfaces – Iterator – ListIterator JCF Iterable interfaces – Iterable – Collection – List JCF Iterable Collections – ArrayList – LinkedList Introduction to Testing

CS-2852 Data Structures, Andrew J. Wozniewicz Iterators An object Traversing a collection Hide implementation In computer science, an iterator is an object that allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation. Wikipedia Definition

CS-2852 Data Structures, Andrew J. Wozniewicz The Iterator Interface import java.util.* interface Iterator { boolean hasNext(); E next(); void remove(); // optional }

CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List – getIterator() public class DoublyLinkedListIterable { public Iterator getIterator() { return new DoublyLinkedListIterator (this); }

CS-2852 Data Structures, Andrew J. Wozniewicz Doubly-Linked List – Iterator public class DoublyLinkedListIterator implements Iterator { DoublyLinkedListIterable theList; Node current; public DoublyLinkedListIterator( DoublyLinkedListIterable list) { theList = list; reset(); } //TODO operations } DEMO

CS-2852 Data Structures, Andrew J. Wozniewicz The ListIterator Interface import java.util.* public interface ListIterator extends Iterator { public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional }

CS-2852 Data Structures, Andrew J. Wozniewicz The ListIterator Interface Expanded import java.util.* public interface ListIterator { boolean hasNext(); E next(); void remove(); // optional public boolean hasPrevious(); public E previous(); public int nextIndex(); public int previousIndex(); public void set(E o); //optional public void add(E o); //optional }

CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators I import java.util.* class IteratorDemo { public static void main(String args[]) { }

CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators II // create an array list ArrayList al = new ArrayList (); // add elements to the array list al.add("Charlie"); al.add("Alpha"); al.add("Echo"); al.add("Bravo"); al.add("Delta"); al.add("Foxtrot"); inside main(String args[])

CS-2852 Data Structures, Andrew J. Wozniewicz The Iterable Interface public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable {... } public interface List extends Collection {... } The Basis for Collection, List Java Collections Framework

CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators III // display contents of al System.out.print(“Content of al: "); Iterator itr = al.iterator(); while (itr.hasNext()) { Object element = itr.next(); System.out.print(element + " "); } inside main(String args[])

CS-2852 Data Structures, Andrew J. Wozniewicz Using Iterators IV // modify objects being iterated ListIterator litr = al.listIterator(); While (litr.hasNext()) { String element = litr.next(); litr.set(element + "+"); } inside main(String args[])

CS-2852 Data Structures, Andrew J. Wozniewicz The List Interface public interface Iterable { Iterator iterator(); } public interface Collection extends Iterable {... } public interface List extends Collection { ListIterator listIterator(); ListIterator listIterator(int index);... } The Basis for Collection, List Java Collections Framework

CS-2852 Data Structures, Andrew J. Wozniewicz ArrayList Import java.util.* List al = new ArrayList (); al.add("Charlie"); al.remove(0); Resizable-array implementation of the List interface. As elements are added to an ArrayList, its capacity grows automatically. Implements all optional list operations. Permits null. Provides methods to manipulate size of the underlying data array. ( stack, queue, or double-ended queue). Java Collections Framework

CS-2852 Data Structures, Andrew J. Wozniewicz LinkedList import java.util.* List intList = new LinkedList (); intList.addLast(42); intList.addFirst(17); Doubly-linked list implementation of the List interface. Implements all optional list operations. Permits null. Provides uniformly named methods to get, remove and insert an element at the beginning and end of the list ( stack, queue, or double-ended queue). Java Collections Framework

CS-2852 Data Structures, Andrew J. Wozniewicz Summary JCF iterator interfaces – Iterator – ListIterator JCF iterable interfaces – Iterable – Collection – List JCF iterable Collections – ArrayList – LinkedList

Questions? Image copyright © 2010 andyjphoto.com