CSC 205 – Java Programming II

Slides:



Advertisements
Similar presentations
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
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.
Computer Science 112 Fundamentals of Programming II List Iterators.
Double-Linked Lists and Circular Lists
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.
– Advanced Programming P ROGRAMMING IN Lecture 16 Interfaces.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
1 Lecture 24 ADT Part V (Linked List Using Iterator) Overview  Utility Classes.  List Iterator.  View of the List Iterator.  Adding to the Head of.
15-Jun-15 Lists in Java Part of the Collections Framework.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
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.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2; 15.3; 16.5 slides adapted from Marty Stepp
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
LinkedList Many slides from Horstmann modified by Dr V.
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.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Java 2 Collections Bartosz Walter Software Engineering II.
Computer Science 209 The Factory Pattern. Collections and Iterators List list1 = new ArrayList (); List list2 = new LinkedList (); Set set1 = new HashSet.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Using Iterators Trey Chandler Burlington Williams High School Mr. Langner’s Class,
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
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.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
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.
CSE 143 Lecture 12: Sets and Maps reading:
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.
University of Limerick1 Collections The Collection Framework.
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
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.
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
EKT472: Object Oriented Programming
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Chapter 5: The List Abstract Data Type
CSE 373 Implementing a Stack/Queue as a Linked List
Efficiency of in Binary Trees
Data Structures Lakshmish Ramaswamy.
Announcements & Review
Incrementing ITP © Ron Poet Lecture 8.
Back to Collections Lists: Sets Maps ArrayList LinkedList
CSE 143 Lecture 27: Advanced List Implementation
Iterator.
Need for Iterators O(n2) this is a common application
Arrays versus ArrayList
slides adapted from Marty Stepp
Recursion B.Ramamurthy 2/23/2019 Ramamurthy.
JCF Collection classes and interfaces
slides created by Alyssa Harding
Iterators Dan Fleck.
Part of the Collections Framework
Data Structures and Algorithms 2/2561
Java Generics & Iterators
Presentation transcript:

CSC 205 – Java Programming II Lecture 29 March 25, 2002

Drawbacks of LinkedList Unsatisfactory performance when accessing elements by index add(4, new Character(‘u’)); first last A c c o n t 1 2 3 4 5 u

Solution: ListIterator Methods defined in ListIterator public boolean hasNext() public boolean hasPrevious() public Object next() public Object previous() public void remove() public void add(Object o) public void set(Object o) public int nextIndex() public int previousIndex ()

Example LinkedList letters = new LinkedList(); ListIterator lItr = letters.listIterator(); lItr.add(new Character('f')); lItr.add(new Character('t')); lItr.previous(); lItr.add(new Character('e')); lItr.add(new Character('r')); lItr.next(); lItr.add(new Character('c')); lItr = letters.listIterator(); lItr.add(new Character('p'));

A perfect Example f t e r f t e r f e r t p e r f e r t first First two add and two previous first Two more add one next e r f t Two more Add, one list- Iterator first e r f e r t first p e r f e r t

Creating ListIterator Creating ListIterator through listIterator methods only listIterator() listIterator(int index)

Using ListIterator Relative positions: one convenient view Interleaf ListIterator positions with element positions first last A c c o n t 1 2 3 4 5 listIterator() listIterator(a.size()) listIterator(0)

Using ListIterator Adding elements itr.add(new Character(‘u’)); u A c first last A c c o n t 1 2 3 4 5 Current position of The listIterator

Using ListIterator After adding ‘u’ into the list, we have itr.next()  ‘n’ Itr.previous()  ‘u’ One problem: this loop never stops lItr = fruits. listIterator(fruits.size()); while (lItr.hasPrevious()) { System.out.println(lItr.previous()); lItr.add("Pears"); }

Using ListIterator This loop works fine fruits.add("Kumquats"); fruits.add("Bananas"); fruits.add("Kiwi"); fruits.add("Apples"); lItr = fruits.listIterator(); while (lItr.hasNext()) { System.out.println(lItr.next()); lItr.add("Pears"); }

Using ListIterator Relative positions: the normal view A c c o n t first last A c c o n t 1 2 3 4 5 listIterator() listIterator(0) listIterator(a.size())

Using ListIterator The next method advances to the next position, but returns the element that had been in the current position Similar to the post-increment operator (i++) The previous method first retreats to the position before the current position, and then returns the element at that retreated-to position Similar to the pre-decrement operator (--i)