Practical Session 4 Java Collections



Similar documents
Data Structures in the Java API

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Class 32: The Java Collections Framework

Java Collection Framework hierarchy. What is Data Structure? Chapter 20 Lists, Stacks, Queues, and Priority Queues

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Variables, Constants, and Data Types

Introduction to Data Structures

Contents. 9-1 Copyright (c) N. Afshartous

Chapter 3: Restricted Structures Page 1

Basic Programming and PC Skills: Basic Programming and PC Skills:

Stacks. Data Structures and Data Types. Collections

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Big O and Limits Abstract Data Types Data Structure Grand Tour.

Project 4 DB A Simple database program

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures and Algorithms

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Introduction to Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Working with Java Collections

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Computing Concepts with Java Essentials

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

J a v a Quiz (Unit 3, Test 0 Practice)

Java Interview Questions and Answers

The Java Collections Framework

The C Programming Language course syllabus associate level

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

CMSC 202H. ArrayList, Multidimensional Arrays

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Analysis of a Search Algorithm

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Visual Basic Programming. An Introduction

C++ INTERVIEW QUESTIONS

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Java Programming Language

CS 111 Classes I 1. Software Organization View to this point:

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Habanero Extreme Scale Software Research Project

Object-Oriented Programming

Data Structures. Level 6 C Module Descriptor

Java from a C perspective. Plan

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Java the UML Way: Integrating Object-Oriented Design and Programming

Java Cheatsheet. Tim Coppieters Laure Philips Elisa Gonzalez Boix

Java Basics: Data Types, Variables, and Loops

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

AP Computer Science AB Syllabus 1

Principles of Software Construction: Objects, Design and Concurrency. Java Collections. toad Fall 2013

LINKED DATA STRUCTURES

Computer. Course Description

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Chapter 8: Bags and Sets

COMPUTER SCIENCE 1999 (Delhi Board)

Computer Programming I

AP Computer Science Java Subset

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Data Structures and Algorithms Lists

: provid.ir

dictionary find definition word definition book index find relevant pages term list of page numbers

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

The Smalltalk Programming Language. Beatrice Åkerblom

JDBC (Java / SQL Programming) CS 377: Database Systems

JAVA COLLECTIONS FRAMEWORK

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Java Crash Course Part I

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Introduction to Java. CS 3: Computer Programming in Java

Java EE Web Development Course Program

CompSci-61B, Data Structures Final Exam

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Two-Dimensional Arrays Summer 2010 Margaret Reid-Miller

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Lab Experience 17. Programming Language Translation

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Database Query 1: SQL Basics

BCS2B02: OOP Concepts and Data Structures Using C++

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

The Interface Concept

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

DATA STRUCTURE - QUEUE

Introduction to Programming System Design. CSCI 455x (4 Units)

Transcription:

Practical Session 4 Java Collections

Outline Working with a Collection The Collection interface The Collection hierarchy Case Study: Undoable Stack The Collections class Wrapper classes

Collection A group of elements. The group size can be changed during run-time. A collection object has many useful methods for manipulating the collection: Inserting elements Deleting elements Copying the collection Iterating over the elements Computing subsets of elements Various types of collections are defined in the standard library: Vector, LinkedList, Stack, ArrayDequeue, PriorityQueue, TreeSet,

Collection Why shouldn t we just use arrays? Arrays are not objects! You cannot invoke methods on arrays. Arrays have fixed size. Arrays are less convenient for representing certain types of collections: Sets Double-ended queues Dictionaries

Working with a collection Definition syntax: Collection <type> colname = new Collection <type>() Example: import java.util.vector; Vector <Car> carsvec = new Vector <Car>(); Car Volvo = new Car(2.0); Car BMW = new Car(2.2); carsvec.add(volvo); carsvec.add(bmw); for (Car c: carsvec) System.out.println( + c);

Working with a collection Example - continued: Car temp = carsvec.elementat(0); carsvec.set(0,carsvec.elementat(1)); carsvec.set(1, temp); for (int i=0; i < carsvec.size(); i++) System.out.println("" + carsvec.elementat(i));

The Collection Interface Collection is an interface in the standard java library. The Collection interface is generic. It accepts a type as a parameter. public interface Collection<E> extends Iterable<E> { int size(); boolean isempty(); boolean contains(object element); boolean add(e element); //optional boolean remove(object element); //optional Iterator<E> iterator(); Object[] toarray(); <T> T[] toarray(t[] a);

The Collection Hierarchy (Partial illustration) Collection List Set Queue

The Collection Hierarchy (Partial illustration) Collection List Set Queue Vector

The Collection Hierarchy (Partial illustration) Collection List Set Queue Vector LinkedList PriorityQueue Stack

Case Study: Undoable Stack Various programs allow the user to undo his operations. The undo operations are performed in reverse order. In such a program, we need to add each operation with its parameters onto the stack.

Program Stack

Program Stack Resize 36

Program Stack Resize 36 Recolor 4 Resize 36

Program Stack Resize 24 Recolor 4 Resize 36 Undo: Resize(36)

Program Stack Resize 24 Recolor 4 Resize 36 Undo: Resize(36) Recolor 4 Resize 36

Undoable Stack Hierarchy

Undoable Stack Hierarchy TextArea -text: String -size: int -color: int + getters + setters

Undoable Stack Hierarchy Operation + perform(arg: int) + getarg() : int TextArea -text: String -size: int -color: int + getters + setters

Undoable Stack Hierarchy Operation + perform(arg: int) + getarg() : int Recolor Resize - color: int - size: int + perform(arg: int) + perform(arg: int) + getarg() : int + getarg() : int TextArea -text: String -size: int -color: int + getters + setters

Undoable Stack Hierarchy Operation 0..n + perform(arg: int) + getarg() : int Recolor Resize - color: int - size: int + perform(arg: int) + perform(arg: int) + getarg() : int + getarg() : int TextArea -text: String -size: int -color: int + getters + setters UndoStack + add (op :Operation) + undo()

The Collections class The Collections class contains static methods that operate on collections: min max reverse sort Example: import java.util.collections; import java.util.vector; Vector <Car> carsvec = new Vector <Car>(); Collections.reverse(carsVec);

Wrapper Classes What happens if we want to create a collection of a primitive type? Collections can be made only of objects. Primitive types are not objects. We use wrapper classes instead. A wrapper class is an object representing a primitive type.

Wrapper Classes Integer Float Double Character Boolean int float double char boolean Example: Integer intobj = new Integer(10); int number = intobj.intvalue();

A collection of wrapper objects Import java.util.vector; Vector <Integer> numvec = new Vector <Integer>(); int size = 10; for (int i = size; i >0; i--) numvec.add(new Integer( i )); Collections.sort(numVec); for (Integer i: numvec) System.out.println( i);