Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays with meaningful indices."— Presentation transcript:

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

2 © Peter Andreae COMP 102 23:2 Menu Limitations on the for each loop Arrays with meaningful indices Assignment 8 Administrivia:

3 © Peter Andreae COMP 102 23:3 Can’t use “foreach” everywhere Can’t use to change the values in the array for (int i = 0 ; i < data.length ; i = i+1) { 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: 1553791824 value: shapes: sh: c: red x: y: c: blue x: y:

4 © Peter Andreae COMP 102 23:4 Arrays with Meaningful Indices Arrays to store a collection: index doesn't mean anything Sometimes, the 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: 012345678 700620460 450380220510480 012345678910 n1n1 n5n5 n8n8 n 10 n7n7 n3n3 n6n6 n4n4 012345678910111213141516 2 9 31 15804100 0123456789 4 89 69091 858687 7 2 85 39214 s1s1 null s5s5 s3s3 s8s8 s2s2 s6s6 s9s9 names: 0123456789101112

5 © Peter Andreae COMP 102 23:5 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: 012345678910111213141516 Don ’ t need to search

6 © Peter Andreae COMP 102 23:6 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: 012345678910111213141516

7 © Peter Andreae COMP 102 23:7 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: 012345678910111213141516

8 © Peter Andreae COMP 102 23:8 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: 012345678910111213141516

9 © Peter Andreae COMP 102 23:9 Arrays of Counts to keep track of counts of numbers we have seen, Scanner sc = new Scanner(new File( … )); int[ ] counts = new int[10000]; int otherCount = 0; while ( sc.hasNextInt() ){ int num = sc.nextInt(); if ( num>=0 && num<10000 ) { counts[num]++; } else { otherCount++; } Initialised with 0 counts: 00000000000 01234567891011121314 … 99999 00000 …

10 © Peter Andreae COMP 102 23:10 Arrays of Booleans looking for duplicates: read file, check and set Issue: keep track of numbers we have seen, efficiently 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<100000 ){ if (numbersSeen[num] ) { System.out.println(num + "is a duplicate"); } } numbersSeen[num] = true; } else{ String junk = sc.next(); } } Initialised with false numbersSeen: fffffffffff 01234567891011121314 … 99999 fffff … Could we do this with words?

11 © Peter Andreae COMP 102 23:11 Assignment 8 BalloonGame Array of Balloon objects Scoring

12 © Peter Andreae COMP 102 23:12 Assignment 8 Genealogy File of data Array of Person db : 0123456789 ID: 6 name: Angel dob: 1805 motherID: -1 fatherID: -1 ID: 0 name: Devin dob: 1810 motherID: -1 fatherID: -1 ID: 5 name: Addison dob: 1907 motherID: 1 fatherID: 7 ID: 1 name: Alex dob: 1840 motherID: 6 fatherID: 0 10 6 Angel 1805 -1 -1 5 Addison 1907 3 7 0 Devin 1810 -1 -1 3 Alex 1840 6 0 4 Jessie 1839 6 0 8 Jordan 1842 6 0 7 Alexis 1843 -1 -1 9 Kasey 1883 -1 8 1 Kelly 1910 9 -1 2 Dominique 1915 1 7 CurrentID:


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

Similar presentations


Ads by Google