C19: Collection Classes (don’t forget to look at all the online code examples)

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.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Dictionaries Chapter Chapter Contents Specifications for the ADT Dictionary Entries and methods Using the ADT Dictionary English Dictionary Telephone.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
1 L43 Collections (3). 2 OBJECTIVES  To use the collections framework interfaces to program with collections polymorphically.  To use iterators to “walk.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
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.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
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.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
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.
TCSS 342, Winter 2005 Lecture Notes
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 13: Queues and Vectors.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
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.
Information and Computer Sciences University of Hawaii, Manoa
111 © 2002, Cisco Systems, Inc. All rights reserved.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.
CPSC 102: Computer Science II Dr. Roy P. Pargas 408 Edwards Hall Office Hours 10:00-11:00 am MWF 2:00-3:00 pm TTh.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Computer Science 209 Software Development Java Collections.
Data structures Abstract data types Java classes for Data structures and ADTs.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
The Java Collections Framework Based on
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
3-February-2003cse Collections © 2003 University of Washington1 Java Collections CSE 403, Winter 2003 Software Engineering
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.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
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.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
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,
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Fundamental of Java Programming
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
CS313D: Advanced Programming Language
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
ArrayLists 22-Feb-19.
Collections Framework
L5. Necessary Java Programming Techniques
Hashing in java.util
Presentation transcript:

C19: Collection Classes (don’t forget to look at all the online code examples)

Arrays (just a remainder) 3-step process: –declare: static public CardPile[] allPiles; –define: allPiles = new CardPile[27 ]; or int[] primes = {2,3,5,7}; –intialize: for(int i = 0; i< allPiles.length; i++) allPiles[i] = new TablePile(i); Bounds are checked! 0.. length-1, else throw IndexOutOfBoundsException Have a uniform type (primitives or classes), but is polymorphic: e.g. Object[] can hold instances of any class Clonable: creates a shallow copy: int[] numbers = primes.clone();

Collections (like Vector,..) Maintain their values as type Object  –Cannot store primitives directly, must use wrapper classes (Integer, Boolean, …) –Must cast back retrieved objects to their original type Can be seen as a linear sequence of elements: support Enumeration interface

Enumerators Uniform way of iterating through the elements of whatever type collection: boolean hasMoreElements() Object nextElement() Always invoke these two in tandem, –Never call nextElement() without first checking hasMoreElements() for truth –Never call nextElement() twice in a row Typical code patterns are: for(Enumeration e = htab.elements(); e.hasMoreElements(); ) System.out.println(e.nextElement); –Or: Enumeration e = htab.elements(); while (e.hasMoreElements()) System.out.println(e.nextElement);

Vector Similar to arrays, but more general operations supported: –(automatically) expandable! Size: size(), isEmpty(), capacity(), setSize(int) Access: contains(Object), firstElement(), lastElement(), elementAt(int) Insert/modify: addElement(Object), setElementAt(Object,int),insertElement(Object,int) Remove: removeElementAt(int),removeElement(Object), removeAllElements() Search: indexOf(Object), lastIndexOf(Object) Misc: clone(), toString()

Use Vector as.. Array: v.setElementAt(v.elementAt(37)+12,5); Stack: addElement(), lastElement(), removeElementAt(v.size()-1) Queue: add to back, remove from front: addElement(), firstElement(), removeElementAt(0) Set: contains(), addElement(), removeElement()

Use Vector as a List Insert and remove at any location, locate insertElementAt(Object,int) removeElementAt(Object,int) Not a linked list: arbitrary inserts/remove might be expensive, need to shift around vector contents

Stack collection Subclass of Vector: Object push(Object) Object peek() Object pop() int size() boolean empty() int search(Object): -1 if not found, 1 for top of stack, … (book is wrong) (cf. chapter 10 for pro/cons “Stack extends Vector”)

BitSet Expandable like Vector, alternative to boolean[], supports more methods (see Sieve example): BitSet(int) constructor, all bits are off (0) initially void set(int) set a bit boolean get(int) get bit status void clear(int) clear a bit (set to 0) void or(BitSet) compute logical-or of two bitsets void and(BitSet) void xor(BitSet) String toString(): nice list of comma-separated on- positions

Abstract class Dictionary Relate arbitrary values to keys: Object get(Object key) Object put(Object key, Object value) Object remove(Object key) Hashtable: subclass, adds useful methods boolean containsKey(Object key) boolean containsValue(Object value) Enumeration elements() Enumeration keys() void clear() ( see Concordance code example)

Properties Subclass of HashTable managing String key/value pairs including storage on disk: void load(InputStream) void save(OutputStream o, String header) –see Properties code example

Order Initially Java had no ordered collections, but since 1.2 is has, using a mechanism similar to the one in described in section 19.8 Look for: Comparator, Comparable, static sort method in class Arrays, SortedMap, …