more arrays primitive vs. reference parameters. arrays as parameters to functions

54
More arrays More arrays Primitive vs. reference Primitive vs. reference parameters. parameters. Arrays as parameters to Arrays as parameters to functions. functions.

Upload: curtis-lewis-willis

Post on 02-Jan-2016

253 views

Category:

Documents


1 download

TRANSCRIPT

More arraysMore arrays

Primitive vs. reference Primitive vs. reference parameters.parameters.

Arrays as parameters to functions.Arrays as parameters to functions.

Recall from our room reservation problem Recall from our room reservation problem how we initialized our hotel array.how we initialized our hotel array.

final intfinal int N = 100; //# of roomsN = 100; //# of rooms//note: there are N rooms which are subscripted 0..N-1.//note: there are N rooms which are subscripted 0..N-1.booleanboolean rooms[] = new boolean[ N ];rooms[] = new boolean[ N ];

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<N; i++) {for (int i=0; i<N; i++) {

rooms[i] = false;rooms[i] = false;}}

Can we put this code in a function that we call from our Can we put this code in a function that we call from our main function?main function?Can we use arrays as function parameters?Can we use arrays as function parameters?

Warning!Warning!

TechnicalTechnical

definitionsdefinitions

ahead.ahead.

Java typesJava types

““There are two kinds of types in the Java There are two kinds of types in the Java programming language:programming language:

1.1. primitive types andprimitive types and

2.2. reference types.”reference types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Java valuesJava values

““There are, correspondingly, two kinds of data There are, correspondingly, two kinds of data values that can be stored in variables, passed as values that can be stored in variables, passed as arguments, returned by methods, and operated arguments, returned by methods, and operated on:on:

1.1. primitive values andprimitive values and

2.2. reference values.”reference values.”

from from http://docs.oracle.com/javase/specs/jls/se7/jls7.phttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdfdf

Primitive typesPrimitive types

booleanbooleanbytebyteshortshort intint longlongcharchar floatfloatdoubledouble

Reference typesReference types

““There are four kinds of reference types: class There are four kinds of reference types: class types, interface types, type variables, and types, interface types, type variables, and array types.”array types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j;int j;

int[] A;int[] A; // A and B are references// A and B are references

Int[] B;Int[] B;

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----10

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i );

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------

-------

-------…-------

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5-------

-------…-------

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5-------

-------…-------

B-----

.

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] );

i-----10

j-----

10 5X

A-----

.

-------5 9-------

-------…-------

B-----

.X

Practical examplePractical example

int i = 10;int i = 10; // i and j are primitives// i and j are primitives

int j = i;int j = i;

j = 5;j = 5;

System.out.println( i );System.out.println( i ); 1010

int[] A = new int [ 10 ];int[] A = new int [ 10 ]; // A and B are references// A and B are references

A[0] = 5;A[0] = 5;

int[] B = A;int[] B = A;

B[0] = 9;B[0] = 9;

System.out.println( A[0] );System.out.println( A[0] ); 99

i-----10

j-----

10 5X

A-----

.

-------5 9-------

-------…-------

B-----

.X

Recall from our room reservation problem Recall from our room reservation problem how we initialized our hotel array.how we initialized our hotel array.

final intfinal int N = 100; //# of roomsN = 100; //# of roomsbooleanboolean rooms[] = new boolean[ N ];rooms[] = new boolean[ N ];//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<N; i++) {for (int i=0; i<N; i++) {

rooms[i] = false;rooms[i] = false;}}

Can we put this code in a function that we call from Can we put this code in a function that we call from our main function?our main function? Yes!Yes!Can we use arrays as function parameters?Can we use arrays as function parameters?

Yes!Yes!

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {//How do we use this new function from main?//How do we use this new function from main?

}}}}

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

//How do we use initializeHotel from main?//How do we use initializeHotel from main?

}}}}

Our hotel reservation system.Our hotel reservation system.

class Hotel {class Hotel {static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );

}}}}

Our hotel reservation system.Our hotel reservation system.class Hotel {class Hotel {

//init our hotel to all empty//init our hotel to all emptystatic void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

//init all rooms to unoccupied//init all rooms to unoccupiedfor (int i=0; i<occupied.length; i++) {for (int i=0; i<occupied.length; i++) {

occupied[i] = false;occupied[i] = false;}}

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );

}}}}

This is an example of a parameter with a reference value. Changes made in initializeHotel to occupied array entries change rooms array entries in main.

Our hotel reservation system.Our hotel reservation system.

How about another function to find the first How about another function to find the first free (available/unoccupied) room?free (available/unoccupied) room? It should return the room # of the first available It should return the room # of the first available

room.room.

If no rooms are available, it should return -1.If no rooms are available, it should return -1.

We will also specify a range of room numbers to We will also specify a range of room numbers to check (we won’t always check the entire hotel).check (we won’t always check the entire hotel).

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Our hotel reservation system:Our hotel reservation system:first free roomfirst free room

//This function returns the number of the first available//This function returns the number of the first available// in the range of [first,last] inclusive.// in the range of [first,last] inclusive.// If no room can be found, -1 is returned.// If no room can be found, -1 is returned.static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

int found = -1;int found = -1;for (int i=first; i<=last; i++) {for (int i=first; i<=last; i++) {

//find the first room that is not occupied//find the first room that is not occupiedif ( !occupied[i] ) {if ( !occupied[i] ) {

found = i;found = i; //remember the free room #//remember the free room #break;break; //terminate the loop//terminate the loop

}}}}return found;return found;

}}

How can we use this new function in How can we use this new function in main to check is a room between # main to check is a room between #

10 and # 20 is available?10 and # 20 is available?

How can we use this new function in main to How can we use this new function in main to check is a room between # 10 and # 20 is check is a room between # 10 and # 20 is

available?available?public static void main ( String args[] ) {public static void main ( String args[] ) {

final int N = 100; //# of roomsfinal int N = 100; //# of rooms//var to indicate occupied/unoccupied rooms//var to indicate occupied/unoccupied roomsboolean rooms[] = new boolean[ N ];boolean rooms[] = new boolean[ N ];

initializeHotel( rooms );initializeHotel( rooms );//check for a room between 10 and 20//check for a room between 10 and 20int rm = firstFree( 10, 20, rooms );int rm = firstFree( 10, 20, rooms );System.out.println( "room # " + rm + " is available" System.out.println( "room # " + rm + " is available"

););}} In the call to firstFree, 10 and 20 are

primitive values, and rooms is a reference value.

How can we use this new function in How can we use this new function in main to check our entire hotel for the main to check our entire hotel for the

first free room?first free room?

How can we use this new function in main to How can we use this new function in main to check our entire hotel?check our entire hotel?

//check for a room in the entire hotel//check for a room in the entire hotel

int r = firstFree( 0, N-1, rooms );int r = firstFree( 0, N-1, rooms );

System.out.println( "room # " + rSystem.out.println( "room # " + r

+ " is available" );+ " is available" );In this call to firstFree, 0 and N-1 are primitive values, and rooms is a reference value.

Passed by value vs. passed by Passed by value vs. passed by referencereference

So what’s the difference?So what’s the difference?

For a For a primitive valueprimitive value, any changes to the , any changes to the variable in the function DO NOT affect the variable in the function DO NOT affect the caller.caller.

For a For a reference valuereference value, any changes in the , any changes in the function to the data to which the reference function to the data to which the reference refers DO affect the caller.refers DO affect the caller.

Passed by value vs. passed by Passed by value vs. passed by reference example.reference example.

static void test ( int A, int B[] ) {static void test ( int A, int B[] ) {A = A + 12;A = A + 12;B[0] = B[0] + 12;B[0] = B[0] + 12;

}}……In main:In main:

int A = 5;int A = 5;

int D[] = new int [2];int D[] = new int [2];D[0] = 5;D[0] = 5;D[1] = 5;D[1] = 5;

test( A, D );test( A, D );System.out.println( "A is " + A + “, D[0] is " + D[0] );System.out.println( "A is " + A + “, D[0] is " + D[0] );

What’s the output?

Remember, A is a primitive value, and D is a reference value.

Passed by value vs. passed by Passed by value vs. passed by reference example.reference example.

static void test ( int A, int B[] ) {static void test ( int A, int B[] ) {A = A + 12;A = A + 12;B[0] = B[0] + 12;B[0] = B[0] + 12;

}}……In main:In main:

int A = 5;int A = 5;

int D[] = new int [2];int D[] = new int [2];D[0] = 5;D[0] = 5;D[1] = 5;D[1] = 5;

test( A, D );test( A, D );System.out.println( "A is " + A + “, D[0] is " + D[0] );System.out.println( "A is " + A + “, D[0] is " + D[0] );

What’s the output?

A is 5, D[0] is 17

test made changes to both A and B[0] but since A is a primitive value, those changes don’t affect A in main.

Since B is a reference value, the changes made by test to B[0] do affect D[0] in main.

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number Write a function that, given a room number and our rooms array, indicates that that and our rooms array, indicates that that room is occupied.room is occupied.

So far we have…So far we have…

class Hotel {class Hotel {

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

So far we have…So far we have…class Hotel {class Hotel {

static void bookARoom ( int rm, boolean rooms[] ) {static void bookARoom ( int rm, boolean rooms[] ) {

……

}}

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

}}

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number and Write a function that, given a room number and our rooms array, indicates that that room is our rooms array, indicates that that room is occupied.occupied.

static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] )

{{

… … ////what do we need to do here?what do we need to do here?

}}

Back to our hotel reservation Back to our hotel reservation system…system…

Write a function that, given a room number and Write a function that, given a room number and our rooms array, indicates that that room is our rooms array, indicates that that room is occupied.occupied.

static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] )

{{

rooms[ rm ] = true;rooms[ rm ] = true;

}}

Back to our hotel reservation Back to our hotel reservation system…system…

//given a room number and our rooms array,//given a room number and our rooms array,// indicate that that room is occupied.// indicate that that room is occupied.static void bookARoom ( int rm, boolean rooms[] )static void bookARoom ( int rm, boolean rooms[] ){{

//it would be wise to check rm here first to determine if//it would be wise to check rm here first to determine if// it is between 1..# of rooms.// it is between 1..# of rooms.rooms[ rm ] = true;rooms[ rm ] = true;

}}……In main:In main:

bookARoom( 1, rooms );bookARoom( 1, rooms );bookARoom( 2, rooms );bookARoom( 2, rooms );bookARoom( 10, rooms );bookARoom( 10, rooms );

More hotel reservation system…More hotel reservation system…

Wouldn’t it be useful to know how many Wouldn’t it be useful to know how many rooms are occupied in our hotel?rooms are occupied in our hotel?

We can write function to do that!We can write function to do that!

So far we have…So far we have…class Hotel {class Hotel {

static void bookARoom ( int rm, boolean rooms[] ) {static void bookARoom ( int rm, boolean rooms[] ) {

……

}}

static void initializeHotel ( boolean occupied[] ) {static void initializeHotel ( boolean occupied[] ) {

……

}}

static int firstFree ( int first, int last, boolean occupied[] ) {static int firstFree ( int first, int last, boolean occupied[] ) {

……

}}

public static void main ( String args[] ) {public static void main ( String args[] ) {

……

}}

static int occupiedCount ( boolean rooms[] ) {static int occupiedCount ( boolean rooms[] ) {

……

}}

}}

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

//check here needed.//check here needed.}}

}}

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

if ( rooms[i] )if ( rooms[i] )++count;++count;

}}return count;return count;

}}

More hotel reservation system…More hotel reservation system…

//function that determines how many rooms//function that determines how many rooms// are occupied in our hotel// are occupied in our hotelstatic int occupiedCount ( boolean rooms[] )static int occupiedCount ( boolean rooms[] ){{

int count = 0;int count = 0;for (int i=0; i<rooms.length; i++) {for (int i=0; i<rooms.length; i++) {

if ( rooms[i] )if ( rooms[i] )++count;++count;

}}return count;return count;

}}

Now, how can we use this new function from main?Now, how can we use this new function from main?

Room in use.Room in use.

In main:In main:

bookARoom( 1, rooms );bookARoom( 1, rooms );

bookARoom( 2, rooms );bookARoom( 2, rooms );

bookARoom( 10, rooms );bookARoom( 10, rooms );

System.out.println( occupiedCount(rooms)System.out.println( occupiedCount(rooms)

+ " in use." );+ " in use." );

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function that returns the percentage of Write a function that returns the percentage of free rooms in the hotel. (Your function should free rooms in the hotel. (Your function should call occupiedCount.)call occupiedCount.)

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function to allow someone to check Write a function to allow someone to check out. Given a room number, this function out. Given a room number, this function should indicate that the room is no longer should indicate that the room is no longer occupied.occupied.

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function to find a block (i.e., Write a function to find a block (i.e., contiguous) of 3 rooms.contiguous) of 3 rooms.

Return the room # of the first room in the block.Return the room # of the first room in the block.If no such block is available, return -1.If no such block is available, return -1.

Hotel reservation system.Hotel reservation system.

Other useful functions include:Other useful functions include:

Write a function called findARoom that finds an Write a function called findARoom that finds an available room.available room.

But we don’t want to simply use firstFree because we’ll But we don’t want to simply use firstFree because we’ll wear out some rooms in our hotel and not use other wear out some rooms in our hotel and not use other rooms at all.rooms at all.

So findARoom should generate a random number and So findARoom should generate a random number and see if that room is free. If it’s not free, findARoom can see if that room is free. If it’s not free, findARoom can then use firstFree.then use firstFree.

RECAPRECAP

Java typesJava types

““There are two kinds of types in the Java There are two kinds of types in the Java programming language:programming language:

1.1. primitive types andprimitive types and

2.2. reference types.”reference types.”

from from http://docs.oracle.com/javase/specs/jls/se7/jlshttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdf7.pdf

Java valuesJava values

““There are, correspondingly, two kinds of data There are, correspondingly, two kinds of data values that can be stored in variables, passed as values that can be stored in variables, passed as arguments, returned by methods, and operated arguments, returned by methods, and operated on:on:

1.1. primitive values andprimitive values and

2.2. reference values.”reference values.”

from from http://docs.oracle.com/javase/specs/jls/se7/jls7.phttp://docs.oracle.com/javase/specs/jls/se7/jls7.pdfdf

Function parameters, and Function parameters, and primitive and reference values.primitive and reference values.

Changes to primitives do not affect the Changes to primitives do not affect the calling function.calling function.

Changes to references (more specifically, Changes to references (more specifically, changes to what the reference refers) do changes to what the reference refers) do affect the calling function. affect the calling function.