Java Generics & Iterators

Slides:



Advertisements
Similar presentations
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++)
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
Professor Evan Korth (adapted from Sun’s collections documentation)
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 The Collection Interface public interface Collection extends Iterable { boolean add(E e); boolean addAll(Collection c); void clear(); boolean contains(Object.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 17 Advanced Java Concepts Data Structure Support.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
05 - Containers DRAFT COPY © S. Uchitel, 2004 Container OrderedDuplicates BagsNOYES SetsNONO ListsYESYES MapsNO.
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.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
Linking Objects Together References are particularly important in computer science because they make it possible to represent the relationship among objects.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
The Java Collections Framework Based on
Java 2 Collections Bartosz Walter Software Engineering II.
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.
CS Ananda Gunawardena.  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Object Oriented Programming in Java Habib Rostami Lecture 7.
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Chapter 5: The List Abstract Data Type
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Programming & Data Structures
Data Structures Lakshmish Ramaswamy.
Introduction to Collections
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
JAVA Collections Framework Set Interfaces & Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Collections
Introduction to Collections
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Collections in Java The objectives of this lecture are:
Introduction to Collections
Collections Framework
slides created by Marty Stepp
Introduction to Collections
JCF Collection classes and interfaces
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
ArrayList.
Iterators Dan Fleck.
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

Java Generics & Iterators Chapter 5 Java Generics & Iterators © 2011 Pearson Addison-Wesley. All rights reserved

The Java Collections Framework Implements many of the more commonly used ADTs Collections framework Unified architecture for representing and manipulating collections Includes Interfaces Implementations Algorithms © 2011 Pearson Addison-Wesley. All rights reserved

Generics JCF relies heavily on Java generics Generics Develop classes and interfaces and defer certain data-type information Until you are actually ready to use the class or interface Definition of the class or interface is followed by <E> E represents the data type that client code will specify © 2011 Pearson Addison-Wesley. All rights reserved

Iterators Iterator JCF provides two primary iterator interfaces Gives the ability to cycle through items in a collection Access next item in a collection by using iter.next() JCF provides two primary iterator interfaces java.util.Iterator java.util.ListIterator Every ADT collection in the JCF have a method to return an iterator object © 2011 Pearson Addison-Wesley. All rights reserved

Iterators ListIterator methods void add(E o) boolean hasNext() boolean hasPrevious() E next() int nextIndex() E previous() int previousIndex() void remove() void set(E o) © 2011 Pearson Addison-Wesley. All rights reserved

The Java Collection’s Framework List Interface JCF provides an interface java.util.List List interface supports an ordered collection Also known as a sequence Methods boolean add(E o) void add(int index, E element) void clear() boolean contains(Object o) boolean equals(Object o) E get(int index) int indexOf(Object o) © 2011 Pearson Addison-Wesley. All rights reserved

The Java Collection’s Framework List Interface Methods (continued) boolean isEmpty() Iterator<E> iterator() ListIterator<E> listIterator() ListIterator<E> listIterator(int index) E remove(int index) boolean remove(Object o) © 2011 Pearson Addison-Wesley. All rights reserved

The Java Collection’s Framework List Interface Methods (continued) E set(int index, E element) int size() List<E> subList(int fromIndex, int toIndex) Object[] toArray() © 2011 Pearson Addison-Wesley. All rights reserved

Stack Implementation using LinkedList ADT © 2006 Pearson Addison-Wesley. All rights reserved

Queue Implementation using LinkedList ADT © 2006 Pearson Addison-Wesley. All rights reserved

Summary Generic class or interface Java Collections Framework Enables you to defer the choice of certain data-type information until its use Java Collections Framework Contains interfaces, implementations, and algorithms for many common ADTs Collection Object that holds other objects Iterator cycles through its contents © 2011 Pearson Addison-Wesley. All rights reserved