Collections & Definite Loops

Slides:



Advertisements
Similar presentations
Collections Chapter Java Collection Frameworks The Java collection framework is a set of utility classes and interfaces. Designed for working with.
Advertisements

Programming with Collections Collections in Java Using Arrays Week 9.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
CSE 115 Week 12 March 31 – April 4, Announcements March 31 – Exam 8 March 31 – Exam 8 April 6 – Last day to turn in Lab 7 for a max grade of 100%,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Week 5 Recap CSE 115 Spring Composition Informally called “has a” Represented in UML with a diamond- headed arc In code: Declare an instance variable.
Basic Definitions Data Structures: Data Structures: A data structure is a systematic way of organizing and accessing data. Or, It’s the logical relationship.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Week 3 Recap CSE 115 – Spring Constructor Special capability of a class that sets up the initial state of the object. Constructor definitions are.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
CSE 115 Week 2 January , Wednesday Announcements Pick up Syllabus if you need one Pick up Syllabus if you need one Recitation Change Form.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Chapter 18 Java Collections Framework
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
Collections Data structures in Java. OBJECTIVE “ WHEN TO USE WHICH DATA STRUCTURE ” D e b u g.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 21 Sets and Maps.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
COP 3503: Programming Fundamentals for CIS Majors 2 Basics.
Chapter 21 Sets and Maps Jung Soo (Sue) Lim Cal State LA.
Sets and Maps Chapter 9.
Objects First with Java CITS1001 week 4
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 5 of Computer Science II
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7 Part 1 Edited by JJ Shepherd
Lesson 5 Functions I A function is a small program which accomplishes a specific task. For example, we invoke (call) the function, sqrt(x), in the library.
Java Collections Overview
CS 106A, Lecture 19 ArrayLists
Dynamic Data Structures and Generics
Collections Not in our text.
Recall What is a Data Structure Very Fundamental Data Structures
Collections and iterators
CSE 1020: The Collection Framework
Dynamic Data Structures and Generics
Sets and Maps Chapter 9.
February , 2009 CSE 113 B.
If-statements & Indefinite Loops
Repetition Statements (Loops) - 2
Review: libraries and packages
Conditional Loops Counted Loops
Web Design & Development Lecture 6
Java String Class String is a class
Abstract Data Types Abstraction is to distill a system to its most fundamental parts. Applying the abstraction paradigm to the design of data structures.
Software Development Techniques
Collections and iterators
Arrays.
Introduction to Java Collection
Presentation transcript:

Collections & Definite Loops CSE 115 Spring 2006 April 10, 12, & 14 2006

Discussion of PacMan Make note of the requirements listed in the lab description. Attend recitations for additional advice and assistance. In class, we will build a game (Tic Tac Toe).

Collections Storage for many objects. Two main types: Bags Associations We will discuss use of collections, you will see how to write your own collection classes in CSE 116 & 250.

Java Collections Framework Java provides implementations for a number of “standard” collections classes in the java.util package. The root interface of the collections hierarchy is Collection.

The Collection<E> interface Note the <E> after the word collection. The <E> indicates that this class can use a generic type (parameterized type). When you create an instance of a class with a generic type, you specify in the <> the actual type for the generic.

Using Generic Types For collections, what you are specifying with the generic type is the type of objects you will be storing in a collection. Eg) I want a bag of cats. When you do this, Java ensures that only objects of the type specified go in and you can be assured that only objects of that type come out.

A usable bag java.util.ArrayList<E> Note the operations that you can perform on this collection. The most important will be creating an instance of the collection, inserting elements, removing elements, and finding if elements are in the collection.

Another important collection operation Iterating over all the elements of a collection and performing some operation with/on each element of the collection. This process can be accomplished by using a special object provided by Java called an iterator. In Java 5, the use of the iterator has been replaced with the for-each loop.

For-each loop Allows access to each element of a collection. Syntax: for(TypeOfElementInCollection giveNameToElement: NameOfCollection) { //write code for what to do with each //element. }

ArrayLists and PacMan Note that in the CSE115.Pacman.BoardPositions class there are ArrayLists for each of the type of cells on the PacMan board. Further explanation will be provided in recitation.

A useful association java.util.HashMap<K, V> Associates a key with a value. Both the key and value are objects. User specifies what type of key and value is used when HashMap is created.

Useful operations with HashMaps put (put a key/value pair in the HashMap) Remove Look up a value using its key Iterating using the for-each loop

Using for-each with HashMaps (note .values()) java.util.HashMap<Position, Cell> _board = new java.util.HashMap <Position, Cell>(); //magic happens to put things into board. for(Cell c: _board.values() { c.draw(); }

The keyword for The for-each is a specialized loop designed to work with collections. for is a keyword in Java that tells us there is a loop. You can create a regular “for-loop” for use in your programs.

Loops (Iteration/Repetition) The ability to do a task repeatedly. The functionality of repetition is most often implemented in programming languages using loops.

Definite Loop The “for-loop” is characterized as a definite loop and is normally used when you know how many times you want a specific task to be performed. It is sometimes referred to as a counting loop.

Entry Test Loop A “for-loop” is also characterized as an entry-test loop. That is, a condition about whether the loop should continue is tested before actually doing the work of the loop.

Syntax of for-loop { //loop body } for (initialization; condition; increment) { //loop body } Usually, the initialization is of a loop counter variable that is checked against a bounds in the condition and is incremented in the increment step.