Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import.

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.
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
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.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
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.
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.
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
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.
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
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
ArrayList, Multidimensional Arrays
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 8 Collection.
The Java Collections Framework Based on
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
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
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
1 Collections Framework A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain:
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
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.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
CSC 205 – Java Programming II Lecture 30 April 3, 2002.
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
C19: Collection Classes (don’t forget to look at all the online code examples)
Object Oriented Programming in Java Habib Rostami Lecture 7.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Fundamental of Java Programming
Sixth Lecture ArrayList Abstract Class and Interface
Implementing ArrayList Part 1
CS313D: Advanced Programming Language
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Collections Framework
L5. Necessary Java Programming Techniques
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Hashing in java.util
Presentation transcript:

Geoff Holmes Palindrome solutions Overview Arrays Collections Enumerators Vector BitSet Stack Dictionary Hashtable Collection Classes (Chapter 19) import java.util.*;

Department of Computer Science2 Palindrome1 solution public class palindrome1 { public static void main (String [] args) { StringBuffer s = new StringBuffer (args[0]); StringBuffer t = new StringBuffer (args[0]); if (s.toString().equals(t.reverse().toString())) System.out.println(s+" is a palindrome"); else System.out.println(s+" is not a palindrome"); }

Department of Computer Science3 Palindrome 2 solution public class palindrome { public static void main (String [] args) { String s = args[0]; for (int i=0; i<s.length()/2; i++) { if (!(s.charAt(i) == (s.charAt(s.length()-1-i)))) { System.out.println(s+" is not a palindrome"); return; } System.out.println(s+" is a palindrome"); }

Department of Computer Science4 Overview Collection classes draw together the work we did last lecture on Object(s) and Enumerators by providing frameworks for storing collections of objects and working our way through them. Wrapper classes are important because collections only deal with objects!

Department of Computer Science5 Arrays – primitive types and size public class array1 { public static void main (String args []) { int [ ] x = {0, 1, 2}; System.out.println(x[3]); } Can Initialize easily using { } This code produces ArrayIndexOutOfBoundsException Have a uniform type (primitives or classes), but is polymorphic: e.g. Object[ ] can hold instances of any class

Department of Computer Science6 Arrays – size not known Need to use new to create array size dynamically (at runtime) public class array2 { public static void main (String args []) { int [ ] x; Integer a = new Integer(args[0]); x = new int [a.intValue()]; for (int i=0; i<a.intValue(); i++) x[i] = i; for (int i=0; i<a.intValue(); i++) System.out.println(x[i]); }

Department of Computer Science7 Arrays – non-primitive types Elements and Array MUST use new public class array3 { public static void main (String args []) { Integer a = new Integer(args[0]); Integer [ ] x = new Integer [a.intValue()]; // have an array of handles at this point for (int i=0; i<a.intValue(); i++) x[i] = new Integer(i); for (int i=0; i<a.intValue(); i++) System.out.println(x[i].intValue()); }

Department of Computer Science8 Arrays of Object public class array4 { public static void main (String args []) { Object [ ] x = new Object [3]; x[0] = new Integer(12); x[1] = new Float ( ); x[2] = new String ("Fred"); }

Department of Computer Science9 Arrays of “real” objects class fred {} public class array2 { public static void main (String args []) { fred [ ] x; // x has no handle! fred [ ] y = new fred [3]; // y has 3 handles set to null fred [ ] z = new fred [3]; for (int i=0; i<z.length; i++) z[i] = new fred(); fred [ ] a = { new fred(), new fred(), new fred() }; //System.out.println("x length = " + x.length); compiler error System.out.println("y length = " + y.length); System.out.println("z length = " + z.length); System.out.println("a length = " + a.length); }

Department of Computer Science10 Collections (Vector, BitSet, Stack and Hashtable) Use arrays for objects and only option for primitives – incr sophistication using a collection class 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

Department of Computer Science11 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);

Department of Computer Science12 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()

Department of Computer Science13 Vector is very flexible 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() List: Insert and remove at any location insertElementAt(Object,int) removeElementAt(Object,int)

Department of Computer Science14 Apples and Oranges Java protects against mixing objects of different types in a vector but only at runtime – this is because we can addElement any Object but must CAST the object back when retrieving (using elementAt) See applesandoranges.java See applesandoranges2.java for enumerator version.

Department of Computer Science15 BitSet – Vector of bits! 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

Department of Computer Science16 Stack – LIFO (pile of papers) 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”)

Department of Computer Science17 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)

Department of Computer Science18 Hashtable A Vector associates numbers with objects If you want to pick objects on another basis then use a Stack – pick the last thing pushed on by popping off. What about looking up objects based on another object? This is what a Dictionary provides (size(), isEmpty(), put, get and remove)

Department of Computer Science19 Concordance – hash array of sets private void enterWord(String word, Integer line) { Vector set = (Vector) dict.get(word); if (set == null) { set = new Vector(); dict.put(word,set); } if (! set.contains(line)) set.addElement(line); }

Department of Computer Science20 Output public void generateOutput(PrintStream output) { Enumeration e = dict.keys(); while (e.hasMoreElements()) { String word = (String) e.nextElement(); Vector set = (Vector) dict.get(word); output.print(word + ": "); Enumeration f = set.elements(); while (f.hasMoreElements()) output.print(f.nextElement() + " "); output.println(""); }