C OMP 110/401 C OLLECTION K INDS Instructor: Prasun Dewan.

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Stacks, Queues, and Linked Lists
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
C OMP 110 A RRAYS Instructor: Jason Carter. 2 O UTLINE for loops Arrays.
C OMP 401 D YNAMIC D ISPATCH AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
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.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
Arrays –Collections (Set, Database, History) Inheritance –inheriting ancestor’s traits –inheriting benefactor’s assets –inheriting instance members( methods/variables)
List Implementations That Use Arrays Chapter 5. 2 Chapter Contents Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation.
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.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
ObjectEditor Prasun Dewan Comp 114. ObjectEditor Automatic user-interface generation. You only write computation code Separate object to do I/O Main just.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Java's Collection Framework
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Arrays And ArrayLists - S. Kelly-Bootle
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Java 2 Collections Bartosz Walter Software Engineering II.
C OMP 401 A DVANCED G ENERICS Instructor: Prasun Dewan.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Copyright (c) Systems and Computer Engineering, Carleton University * Object-Oriented Software Development Unit 13 The Collections Framework.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
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.
C19: Collection Classes (don’t forget to look at all the online code examples)
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();
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
T YPES OF M ODELS Prasun Dewan Department of Computer Science University of North Carolina at Chapel Hill Code available at:
C OMP 401 D YNAMIC D ISPATCH AND V IRTUAL AND A BSTRACT M ETHODS Instructor: Prasun Dewan.
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Java Arrays & Strings.
Recitation 7 October 7, 2011.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Recitation 10 November 3, 2011.
ArrayLists 22-Feb-19.
Collections Framework
Web Design & Development Lecture 6
Presentation transcript:

C OMP 110/401 C OLLECTION K INDS Instructor: Prasun Dewan

2 P REREQUISITE Arrays Collections Implementation

3 S TATIC VS. D YNAMIC S TRUCTURES Static Beans have fixed number of properties Arrays have fixed number of elements Though an array variable can be assigned arrays of different sizes

4 H ISTORY public interface StringHistory { public void addElement(String element); public int size(); public String elementAt( int index); }

5 D ATABASE public interface StringDatabase { //from history public int size(); public void addElement(String element); public String elementAt( int index); //additional methods public void removeElement(String element); public boolean member(String element); public void clear(); } Do we need a history if we have a database? Yes, principle of least privilege

6 P RINCIPLE OF L EAST P RIVILEGE Do not give a user of some code more rights than it needs Code is easier to change Need to learn less to use code Less likelihood of accidental or malicious damage to program Like hiding engine details from car driver

7 U SING D ATABASE AS H ISTORY public interface StringDatabase { //from history public int size(); public void addElement(String element); public String elementAt( int index); //additional methods public void removeElement(String element); public boolean member(String element); public void clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database

8 C O -E XISTENCE public interface StringDatabase extends StringHistory { //additional methods public void removeElement(String element); public boolean member(String element); public void clear(); } Programmer would be able to perform inappropriate operations on a logical history implemented physically as a database

9 V ECTOR : G ENERAL O BJECT C OLLECTION public final int size(); public final Object elementAt(int index); public final void addElement(Object obj) ; public final void setElementAt(Object obj, int index); public final void insertElementAt(Object obj, int index); public final boolean removeElement(Object obj); public final void removeElementAt( int index); public final int indexOf(Object obj); … Do we need other collections if we have Vector Yes, principle of least privilege Yes, implementation considerations

10 C LASS A RRAY L IST A ND V ECTOR (L IST ) public final int size(); public final Object get(int index); public final void add(Object obj) ; public final void set(int index, Object obj); public final void insert( int index, Object obj); public final boolean remove(Object obj); public final void remove( int index); public final int indexOf(Object obj); … Vector has ArrayList (List) methods plus the additional original methods in the previous slides Can add arbitrary objects to these collections

11 A RRAY L IST AND V ECTOR U SER import java.util.ArrayList; import java.util.List; import java.util.Vector; public class VectorArrayListUser { public static void main (String[] args) { List names = new Vector(); List grandSlams = new ArrayList(); names.add("Nadal"); grandSlams.add(11); names.add("Federer"); grandSlams.add(17); names.add(“Borg"); grandSlams.add(11); names.add(“Sampras"); grandSlams.add(14); } Primitive values converted to corresponding wrapper objects

12 V ISUALIZING C OLLECTIONS public static void main (String[] args) { StringHistory stringHistory = new AStringHistory(); stringHistory.addElement("James Dean"); stringHistory.addElement("Joe Doe"); stringHistory.addElement("Jane Smith"); stringHistory.addElement("John Smith"); ObjectEditor.edit(stringHistory); } public interface StringHistory { public void addElement(String element); public int size(); public String elementAt( int index); }

13 C ONVENTIONS FOR V ARIABLE -S IZED C public interface C{ public T elementAt ( int index); public int size(); public Any setElementAt(T t, int index); … } Arbitrary Type. Read methods Write method (optional) Convention based on Vector Unconstrained Type (void or T in practice)

public interface C { public T get ( int index); public int size(); public Any set ( int index) T 2); … } A LTERNATIVE C ONVENTIONS FOR V ARIABLE -S IZED C OLLECTION Arbitrary Type. Read methods Write method (optional) Unconstrained Type (void or T in practice) Convention based on ArrayList

15 R EAD VS. W RITE M ETHODS Read Methods Used to get components of object Getter methods size(), elementAt() Write Methods Used to change components of object Setter methods addElement(), removeElement(), setElementAt() some used by Object Editor Distinction independent of conventions Conventions used in Object Editor

16 C ONVENTIONS FOR V ARIABLE -S IZED C OLLECTION public interface PointHistory { public void addElement ( int x, int y); public Point elementAt ( int index); public int size(); } Read Methods Write Method not recognized by OE Arbitrary Type

17 AP OINT H ISTORY Variable-sized Collection History Methods added to menu associated with class Graphic elements of dynamic collections added at their (X, Y) locations

18 I MPORTANT V ARIABLE S IZED C OLLECTIONS Variable-sized collections History Orders elements Allows retrieval, addition but not replacement or deletion Point History, String History Database May order elements Allows retrieval, search, addition, insertion, and unconstrained removal StringDatabase (did not but should have supported insertion) Sets No duplicates Other collection kinds?

19 S TACK : L AST IN F IRST O UT public interface StringStack { public boolean isEmpty(); public String getTop(); public void push(String element); public void pop(); } public interface StringStack { public boolean isEmpty(); public String getTop(); public void push(String element); public void pop(); } StringStack stringStack = new AStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); System.out.println(stringStack.getTop()); stringStack.pop(); System.out.println(stringStack.getTop()); John Smith Jane Smith

20 Q UEUE : F IRST IN F IRST O UT public interface StringStack { public boolean isEmpty(); public String getTop(); public void push(String element); public void pop(); } public interface StringQueue { public boolean isEmpty(); public String getHead(); public void enqueue(String element); public void dequeue(); } StringQueue stringQ = new AStringQueue(); stringQ.enqueue("James Dean"); stringQ.enqueue("Joe Doe"); stringQ.enqueue("Jane Smith"); stringQ.enqueue("John Smith"); System.out.println(stringStack.getHead()); stringQ.dequeue(); System.out.println(stringStack.getHead()); James Dean Joe Doe

21 D ISPLAYING S TACK (LIFO) public interface StringStack { public boolean isEmpty(); public String getTop(); public void push(String element); public void pop(); } StringStack stringStack = new AStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); ObjectEditor.edit(stringStack); Does not provide read methods for reading all elements

22 D ISPLAYING T RANSPARENT S TACK (LIFO) public interface TransparentStringStack { public int size(); public String get(int index); public void push(String element); public void pop(); } StringStack stringStack = new AStringStack(); stringStack.push("James Dean"); stringStack.push("Joe Doe"); stringStack.push("Jane Smith"); stringStack.push("John Smith"); bus.uigen.ObjectEditor.edit(stringStack); Provides read methods following OE collection conventions Can provide additional method for top value

23 A RRAY L IST AND V ECTOR U SER import java.util.ArrayList; import java.util.List; import java.util.Vector; public class VectorArrayListUser { public static void main (String[] args) { List names = new Vector(); List grandSlams = new ArrayList(); names.add("Nadal"); grandSlams.add(11); names.add("Federer"); grandSlams.add(17); names.add("Djokovic"); grandSlams.add(5); names.add(“Borg"); grandSlams.add(11); } What kind of dynamic structure is being simulated?

24 I NDEXED C OLLECTIONS Each element identified by a unique index Like array indices, collection indices are consecutive Highest index – Lowest Index = size -1 public interface TransparentStringStack { public int size(); public String get(int index); public void push(String element); public void pop(); }

25 T ABLES Each element identified by a unique object called a key Usually strings are used as keys

26 H ASH M AP (I MPLEMENT M AP ) // associates key with value, returning last value associated with key public final Object put (Object key, Object value); // returns last value associated with key, or null if no association public final Object get (Object key);

27 H ASH M AP U SE public static void main (String[] args) { Map aMap = new HashMap(); aMap.put("Nadal", 10); aMap.put("Federer", 17); aMap.put("Sampras", 14); System.out.println(aMap.get("Nadal")); System.out.println(aMap.get("nadal")); aMap.put("Nadal", 11); System.out.println(aMap.get("Nadal")); ObjectEditor.edit(aMap); }

28 OE C ONVENTIONS F OR T ABLE Necessary but not sufficient to displays all keys and elements // associates key with value, returning last value associated with key public put ( key, value); // returns last value associated with key, or null if no association public get ( key); // optional, removes associated value, and returns it or null public remove( key);

29 E XTRA S LIDES

30 INDEX O F (S TRING ELEMENT ) James Dean Joe Doe Jane Smith arraysize indexOf(“James Dean”); 0 index Jane Smith 4 John Smith

31 V ISUALIZATION OF V ARIABLE -S IZED C OLLECTIONS public interface StringHistory { public void addElement(String element); public int size(); public String elementAt( int index); } public interface StringDatabase { //from history public int size(); public void addElement(String element); public String elementAt( int index); //additional methods public void removeElement(String element); public boolean member(String element); public void clear(); }

32 S TRING D ATABASE C OMMANDS

33 G ENERIC L IST : V ECTOR java.util.List strings = new java.util.Vector(); James Dean Joe Doe strings.add“James Dean”) strings.add(“Joe Doe”)

34 A RRAY L IST ( JAVA. UTIL.A RRAY L IST ) AND L IST ( JAVA. UTIL.L IST ) java.util.List strings = new java.util.ArrayList(); James Dean Joe Doe strings.add(“James Dean”) strings.add(“Joe Doe”)

35 V ISUALIZATION OF V ARIABLE -S IZED C OLLECTIONS public class AStringDatabase implements StringHistory { public final int MAX_SIZE = 50; String[] contents = new String[MAX_SIZE]; int size = 0; public int size() { return size;} public String elementAt ( int index) { return contents[index]; } boolean isFull() { return size == MAX_SIZE; } public void addElement(String element) { …} public void removeElement(String element) {…}; public boolean member(String element) {…}; public void clear() {…}; }

36 I MPORTANT V ARIABLE S IZED C OLLECTIONS Variable-sized collections History Orders elements Allows retrieval, addition but not replacement or deletion Point History, String History Database May order elements Allows retrieval, search, addition, insertion, and unconstrained removal StringDatabase (did not but should have supported insertion)