Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
MiniDraw Testing COMP 102 # T1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Creating and using Objects.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Arrays COMP 102.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, VUW Indexing Large Data COMP
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Writing Methods Using if, loops, and variables to implement algorithms Copyright © 2012 Pearson Education, Inc.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Patterns with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Files COMP 102.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington More Interfaces.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington “For each” & Patterns with.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on Files COMP 102 # 14.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
1 Intro to Computer Science Arrays Instructor: Ms. Catherine Stocker Teaching Assistants: Alex, Katie, Siraaj, Isaiah, Allison, Thibault University of.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
An introduction to arrays, continued. Recall from last time… public static void main ( String args[] ) { //define number of rooms final int N = 100; //define.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
COMP More About Arrays Yi Hong June 05, 2015.
2011-T1 Lecture 12 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More on ArrayLists COMP 102.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Implementing ArrayList Part 1
COMPUTER 2430 Object Oriented Programming and Data Structures I
Building Java Programs
CSC1401 Input and Output (with Files)
Building Java Programs
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices COMP 102 # T1

© Peter Andreae Menu Arrays with Meaningful Indices Assignment 8 BalloonGame Genealogy Administrivia: Friday: IT careers fair. 12-2, AM foyer Test: pick up from School office (9am-2pm, Tue-Fri) COMP :2

© Peter Andreae Can’t use “foreach” everywhere Can’t use to change the values in the array for (int i = 0 ; i < data.length ; i = i++) { data[ i ] = data[i] * 2; } for (int value : data) { value = value * 2; } If array contains objects, you can modify the objects: (but not change them!) for (Shape sh : shapes) { sh.setColor(Color.white); } data: value: shapes: sh: c: red x: y: c: blue x: y: COMP :3

© Peter Andreae Arrays with Meaningful Indices Arrays to store a collection: index doesn't mean anything Sometimes, index represents meaningful information array of guest names for hotel rooms (numbered 1 to MaxRoom) house info for each house on street count of occurrences of each number image p1p1 count: 7 p5p5 p3p3 p8p8 p2p2 p6p6 p4p4 data: n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n s1s1 null s5s5 s3s3 s8s8 s2s2 s6s6 s9s9 names: COMP :4

© Peter Andreae Hotel Register initialise empty private String[ ] guests = new String[MaxRoom+1]; // gives indexes from 0 to MaxRoom, ignore index 0 assign to room public void assign(String name; int room){ this.guests[room] = name; } look up to see if empty public boolean isEmpty(int room){ return (this.guests[room]==null); } n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n4 guests: Don ’ t need to search COMP :5

© Peter Andreae Hotel Register: remove Checkout guest from room /* returns true if name was checked out of room successfully, false otherwise */ public boolean checkout(String name, int room){ if ( this.guests[room].equals(name) ){ this.guests[room] = null; return true; } return false; } if ( this.guests[room] != null && this.guests[room].equals(name) ) … if ( name.equals(this.guests[room]) ) … What ’ s the problem? How do we fix it? n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n4 guests: COMP :6

© Peter Andreae Alternative Checkout guest: search and remove: /* returns true if name was checked out successfully, false otherwise */ public boolean checkout(String name){ for (int r=1; r<this.guests.length; r++){ // or <=MaxRoom if (this.guests[r] != null ){ if (this.guests[r].equals(name) { this.guests[r] = null; return true; } return false; } n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n4 guests: COMP :7

© Peter Andreae Find an empty room Find the index of an empty room public int findEmpty(int room){ for (int r=1; r<this.guests.length; r++){// or <=MaxRoom if (this.guests[r]==null) { return r; } } return -1; } Check a guest into an empty room (return room number) public void checkIn(String name){ this.guests[ this.findEmpty() ] = name; } public boolean checkIn(String name){ int r = this.findEmpty(); if (r < 0) { return false; } this.guests[r] = name; return true; } What ’ s the problem? How do we fix it? n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n4 guests: COMP :8

© Peter Andreae Arrays of Counts (for histogram) to keep track of counts of numbers we have seen Scanner sc = new Scanner(new File(… )); int [ ] counts= new int[maxValue+1]; int otherCount= 0; while ( sc.hasNextInt() ){ int num = sc.nextInt(); if ( num>=0 && num<=maxValue ) { counts[num] ++; } else { otherCount++; } … otherCount: max … 0 counts: COMP :9

© Peter Andreae Arrays of Booleans keeping track of numbers we have seen, efficiently looking for duplicates: read file, check and set Scanner sc = new Scanner(new File( … )); boolean[ ] numbersSeen = new boolean[100000]; while ( sc.hasNext() ){ if ( sc.hasNextInt() ){ int num = sc.nextInt(); if ( num>=0 && num< ){ if (numbersSeen[num] ) { System.out.println(num + "is a duplicate"); } numbersSeen[num] = true; } else { sc.next(); } } Initialised with false fffffffff … fffffff numbersSeen: … COMP :10

© Peter Andreae Assignment 8 BalloonGame Array of Balloon objects Remove from array when popped Scoring: COMP :11

© Peter Andreae Balloon Class: touching public class Balloon{ private int radius = 10; private int centerX, centerY; private Color color; /** Returns true if this ball is touching the other ball, and false otherwise */ public boolean colliding(Balloon other){ double dx = other.centerX - this.centerX; double dy = other.centerY - this.centerY; double dist = other.radius + this.radius; return (Math.hypot(dx, dy) <= dist); } COMP :12

© Peter Andreae Assignment 8 Genealogy File of genealogical data Array of Person Current 10 6 Angel Alex Addison Devin Jordan Jessie Alexis Kasey Kelly Dominique db: ID: 6 name: Angel dob: 1805 motherID: -1 fatherID: -1 ID: 3 name: Alex dob: 1840 motherID: 6 fatherID: 0 ID: 5 name: Addison dob: 1907 motherID: 3 fatherID: 7 ID: 0 name: Devin dob: 1810 motherID: -1 fatherID: -1 COMP :13

© Peter Andreae Genealogy COMP :14