Collections.

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
Catie Welsh March 28,  Lab 7 due Friday, April 1st, 1pm 2.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Collections. The Plan ● Why use collections? ● What collections are available? ● How are the collections different? ● Examples ● Practice.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
int [] scores = new int [10];
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CMSC 202 ArrayList Aug 9, 2007.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lecture contents Looping Arrays For each loop while do while for
Arrays Chapter 7.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter 8 Multidimensional Arrays
Two-Dimensional Arrays
Computer Programming BCT 1113
Chapter 19 Java Data Structures
Foundations of Programming: Arrays
University of Central Florida COP 3330 Object Oriented Programming
Chapter 8 Arrays Objectives
Motivations Thus far, you have used one-dimensional arrays to model linear collections of elements. You can use a two-dimensional array to represent a.
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Chapter 8 Multidimensional Arrays
Arrays, For loop While loop Do while loop
Arrays An Array is an ordered collection of variables
Can store many of the same kind of data together
Example: Finding the Mode
Chapter 8 Multidimensional Arrays
Chapter 8 Arrays Objectives
CMSC 202 ArrayList Aug 9, 2007.
int [] scores = new int [10];
Declaration, assignment & accessing
ArrayLists.
Arrays 6-Dec-18.
Sets, Maps and Hash Tables
Can store many of the same kind of data together
Object Oriented Programming in java
Chapter 8 Multidimensional Arrays
Arrays .
CMSC 202 ArrayList Aug 9, 2007.
Multidimensional Arrays
Chapter 8 Multidimensional Arrays
Arrays Chapter 7.
int [] scores = new int [10];
Announcements Lab 6 was due today Lab 7 assigned this Friday
Can store many of the same kind of data together
Single-Dimensional Arrays chapter6
Arrays ICS2O.
Chapter 8 Multidimensional Arrays
Introduction to Data Structure
Loops and Arrays in JavaScript
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 8 Arrays Objectives
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Arrays 2-May-19.
Java: Variables, Input and Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
How do you do the following?
Chapter 8 Multidimensional Arrays
Arrays.
Looping Structures.
Introduction to Java Collection
Presentation transcript:

Collections

The Plan Why use collections? What collections are available? Accessing the elements of a collection? Examples Practice

Why use collections? Consider the code below. What if you wanted 1000 scores? Why is this code not designed well? int score0, score1, score2, score3, ..., score100; score0 = input.nextInt(); score1 = input.nextInt(); ... score100 = input.nextInt(); int sum = score0 + score1 + score2 + ... + score100; double average = sum / 100.0;

Collections & Loops Recall: Loops group repeatedly executed code for uniformity make the number of repetitions easily changeable can be combined with selection to make more complex algorithms

Collections Enable Easily declaring any number of variables Referring to each variable in the collection Grouping similar variables under one name Grouping similar code that acts on the variables Changing the number of variables easily

Why use collections? The code below uses an array to average the 100 scores. What change would make it do 1000 scores? int[] scores = new int[100]; double sum = 0; for (int i = 0; i < scores.length; i++) { scores[i] = input.nextInt(); sum += scores[i]; } double average = sum / scores.length;

What a Collection looks like score scores[0] 45 scores is an array scores[i]is an int scores[1] 76 scores[2] 44 scores[3] 87 scores[4] 98 scores[5] 56 arrays are only one way to collect variables scores[6] 77 ... scores[n-2] 62 scores[n-1] 92

What collections are available? Arrays java.util.Collection ArrayList LinkedList HashSet LinkedHashSet java.util.Map HashMap TreeMap

Arrays Store primitives or particular Objects Size is immutable Contain length field Is an Object Indexed 0 to length-1 Can generate ArrayIndexOutOfBoundsException

ArrayLists Generic, so must specify what kind of thing to hold Size is typically dynamic Has a size() method Is an Object Indexing varies Has toArray(Object[]) method for converting to an array.

Using an ArrayList Can hold any number of scores, does not need to be known beforehand: Note, must hold Integer objects instead of int primitives --- usually not a problem ArrayList<Integer> scores = new ArrayList<Integer>(); double sum = 0; for (int i = 0; i < 100; i++) { scores.add(input.nextInt()); sum += scores.get(i); } double average = sum / scores.size();

Enhanced for loop Works for any kind of collection Simpler syntax for accessing each variable in the collection: // given array scores, with each value initialized double sum = 0; for (int current : scores) { sum += current; } // given ArrayList scores, with each value initialized sum = 0; for (Integer current : scores) { sum += current; }

Practice Declare an array of integers Initialize the array to be able to hold 10 integers Set the values in the array to be the first ten squares (i.e. 1, 4, 9, 16, 25 ...) Sum the values Output the average Alter your code to do the first 100 integers instead

More Practice Change the code in pong so that the paddles and walls are stored in a collection instead of individual variables Play wackadot with a random number of enemy dots (e.g., from 3 to 10) set at the beginning of each game