Data Structures Lakshmish Ramaswamy.

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Advertisements

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.
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.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
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.
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.
(c) University of Washington14-1 CSC 143 Java Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
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.
1.0tCopyright © 1998 Purple Technology, Inc. 1 Java Collections Framework Authored by Alex Chaffee Copyright © 1998 Purple Technology, Inc. All rights.
The Java Collections Framework Based on
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
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.
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.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Collections Dwight Deugo Nesa Matic
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Java Collections CHAPTER 3-February-2003
An Array-Based Implementation of the ADT List
EKT472: Object Oriented Programming
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists Chapter 5 (continued)
Programming & Data Structures
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
Introduction to Collections
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
ArrayList Collections.
EE 422C Sets.
Collections in Java The objectives of this lecture are:
Computer Science and Engineering
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
Introduction to Collections
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Linked Lists Chapter 5 (continued)
ArrayLists.
ArrayLists 27-Apr-19.
ArrayList.
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Presentation transcript:

Data Structures Lakshmish Ramaswamy

Data Structures Representation of data and set of operations Different data structures permit different operations with different costs Insertion, deletion are common operations Goal – Component reuse Implement once use multiple times Example – Filing cabinet, Shelf, Drawer

Generic Protocol

Collection Interface Represents group of objects called elements of a specific type Ordered or unordered Operations boolean isEmpty() int size() boolean add(AnyType x) boolean contains(Object x) boolean remove(Object x) Void clear() Object[] toArray() Java.util.Iterator<AnyType> iterator()

Core Collection Interfaces True collections Can be manipulated as collections (have collection-view operations) Java Collections Framework

Iterator Pattern Consider a collection of objects stored in an array How to print the array elements for(int i = 0; i < (n-1); i++) System.out.println(v[i]); i controls the iteration i ranges from 0 to (n-1) – all elements in the array i is an iterator

Iterator Design Iterator class encapsulates position inside of a collection Provides methods to step through elements for(itr = v.first(); itr.isValid(); itr.advance()) System.out.println(itr.getData())

Generic Iterator Interface

Sample Iterator

Iterator Example

Implementation of Iterator Interface

Printing Elements of Collection

interface List extends Collection Lists are collections where the elements have an order Each element has a definite position (first, second, third, …) Positions are generally numbered from 0 Duplicates may be allowed methods of List: Object get(int pos) – return element at position pos boolean set(int pos, Object elem) – store elem at position pos boolean add(int pos, Object elem) – store elem at position pos; slide elements at position pos to size( )-1 up one position to the right Object remove(int pos) – remove item at given position; shift remaining elements to the left to fill the gap; return the removed element int indexOf(Object o) – return position of first occurrence of o in the list, or -1 if not found Java Collections Framework

ListIterator The iterator( ) method for a List returns an instance of ListIterator Can also send listIterator(int pos) to get a ListIterator starting at the given position in the list ListIterator returns objects in the list collection in the order they appear in the collection Supports additional methods: hasPrevious( ), previous( ) – for iterating backwards through a list set(Object o) – to replace the current element with something else add(Object o) – to insert an element after the current element Java Collections Framework

Some examples: a.set(i,a.get(j).times(a.get(k)) What is an equivalence of the following expression: a[i] = a[j].times(a[k]) for List? a.set(i,a.get(j).times(a.get(k)) Write a method to swap two indexed values in a List. private static void swap(List a, int i, int j) { Object tmp = a.get(i); a.set(i, a.get(j)); a.set(j, tmp); } Java Collections Framework

Some examples: Write a method to randomly shuffle a list (using swap) public static void shuffle(List list, Random rnd) { for (int i=list.size(); i>1; i--) swap(list, i-1, rnd.nextInt(i)); } Java Collections Framework