cs102--object oriented programming lecture 6: – the arrays class – multi-dimensional arrays...

32
CS102--Object Oriented Programming • Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li

Post on 21-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

CS102--Object Oriented Programming

bull Lecture 6

ndash The Arrays class ndash Multi-dimensional arrays

Copyright copy 2008 Xiaoyan Li

Programming tips Commentsdocumentations

bull Document general information about author date function of your programclass at the very beginningndash ndash ndash

bull Make comments for methods and instance variables

Programming tips interaction with users

bull Display message telling the user what to do if your programming is expecting input for the user

bull Echo input to make sure userrsquos input is correctly loaded

bull Check whether userrsquos input is valid

Review on arraysbull 1 When do you use an arraybull 2 What is the base type of an arraybull 3 How do you create an arraybull 4 What are the two ways to initialize an arraybull 5 How to access each element in an arraybull 6 How do you check whether two arrays contain the same

elementsbull 7 Can arrays be parameters for your methodbull 8 Can you return an array in your methodbull 9 What is selection sort Can you implement the selection

sort algorithm in Java

Question Is an Array of Characters a String

bull An array of characters is conceptually a list of characters and so is conceptually like a string

bull However an array of characters is not an object of the class Stringchar[] a = A B CString s = a Illegal

bull An array of characters can be converted to an object of type String however

6-5Copyright copy 2008 Pearson Addison-Wesley All rights reserved

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Programming tips Commentsdocumentations

bull Document general information about author date function of your programclass at the very beginningndash ndash ndash

bull Make comments for methods and instance variables

Programming tips interaction with users

bull Display message telling the user what to do if your programming is expecting input for the user

bull Echo input to make sure userrsquos input is correctly loaded

bull Check whether userrsquos input is valid

Review on arraysbull 1 When do you use an arraybull 2 What is the base type of an arraybull 3 How do you create an arraybull 4 What are the two ways to initialize an arraybull 5 How to access each element in an arraybull 6 How do you check whether two arrays contain the same

elementsbull 7 Can arrays be parameters for your methodbull 8 Can you return an array in your methodbull 9 What is selection sort Can you implement the selection

sort algorithm in Java

Question Is an Array of Characters a String

bull An array of characters is conceptually a list of characters and so is conceptually like a string

bull However an array of characters is not an object of the class Stringchar[] a = A B CString s = a Illegal

bull An array of characters can be converted to an object of type String however

6-5Copyright copy 2008 Pearson Addison-Wesley All rights reserved

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Programming tips interaction with users

bull Display message telling the user what to do if your programming is expecting input for the user

bull Echo input to make sure userrsquos input is correctly loaded

bull Check whether userrsquos input is valid

Review on arraysbull 1 When do you use an arraybull 2 What is the base type of an arraybull 3 How do you create an arraybull 4 What are the two ways to initialize an arraybull 5 How to access each element in an arraybull 6 How do you check whether two arrays contain the same

elementsbull 7 Can arrays be parameters for your methodbull 8 Can you return an array in your methodbull 9 What is selection sort Can you implement the selection

sort algorithm in Java

Question Is an Array of Characters a String

bull An array of characters is conceptually a list of characters and so is conceptually like a string

bull However an array of characters is not an object of the class Stringchar[] a = A B CString s = a Illegal

bull An array of characters can be converted to an object of type String however

6-5Copyright copy 2008 Pearson Addison-Wesley All rights reserved

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Review on arraysbull 1 When do you use an arraybull 2 What is the base type of an arraybull 3 How do you create an arraybull 4 What are the two ways to initialize an arraybull 5 How to access each element in an arraybull 6 How do you check whether two arrays contain the same

elementsbull 7 Can arrays be parameters for your methodbull 8 Can you return an array in your methodbull 9 What is selection sort Can you implement the selection

sort algorithm in Java

Question Is an Array of Characters a String

bull An array of characters is conceptually a list of characters and so is conceptually like a string

bull However an array of characters is not an object of the class Stringchar[] a = A B CString s = a Illegal

bull An array of characters can be converted to an object of type String however

6-5Copyright copy 2008 Pearson Addison-Wesley All rights reserved

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Question Is an Array of Characters a String

bull An array of characters is conceptually a list of characters and so is conceptually like a string

bull However an array of characters is not an object of the class Stringchar[] a = A B CString s = a Illegal

bull An array of characters can be converted to an object of type String however

6-5Copyright copy 2008 Pearson Addison-Wesley All rights reserved

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

An Array of Characters Is Not a String

bull The class String has a constructor that has a single parameter of type char[]String s = new String(a)ndash The object s will have the same sequence of characters as

the entire array a (ABC) but is an independent copybull Another String constructor uses a subrange of a

character array insteadString s2 = new String(a02)ndash Given a as before the new string object is AB

6-6Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Arguments for the Method main

bull The heading for the main method of a program has a parameter for an array of Stringndash It is usually called args by convention

public static void main(String[] args)

ndash Note that since args is a parameter it could be replaced by any other non-keyword identifier

bull If a Java program is run without giving an argument to main then a default empty array of strings is automatically provided

6-7Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Arguments for the Method mainbull Here is a program that expects three string

argumentspublic class SomeProgram public static void main(String[] args) Systemoutprintln(args[0] + + args[2] + args[1])

bull Note that if it needed numbers it would have to convert them from strings first

6-8Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Arguments for the Method mainbull If a program requires that the main method be provided an

array of strings argument each element must be provided from the command line when the program is run

java SomeProgram Hi there

ndash This will set args[0] to Hi args[1] to and args[2] to there

ndash It will also set argslength to 3

bull When SomeProgram is run as shown its output will beHi there

6-9Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)

6-10Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Compare two arrays

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

equalsArraypublic static boolean equalsArray(int[] a int[] b) if (alength = blength) return false else int i = 0 while (i lt alength) if (a[i] = b[i]) return false i++ return true

6-11Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

The Arrays class

bull In javautil packagebull contains various static methods for manipulating

arrays (such as sorting and searching) ndash public static int binarySearch(int[] a int key)

Searches the specified array of ints for the specified value using the binary search algorithm

ndash pubic static boolean equals(int[] a int[] a2) Returns true if the two specified arrays of ints are equal to one another

ndash public static void fill(int[] a int val) Assigns the specified int value to each element of the specified array of

ints

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

The Arrays class

bull More methods

ndash public static void sort(int[] a) Sorts the specified array of ints into ascending numerical order

ndash public static void sort(int[] a int fromIndex int toIndex) Sorts the specified range of the specified array of ints into ascending

numerical order

Exercise Write a piece of code that tests two integer arrays to see if they are equal using the Arrays class

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Exercise

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Multidimensional Arraysbull It is sometimes useful to have an array with more

than one index

bull Multidimensional arrays are declared and created in basically the same way as one-dimensional arraysndash You simply use as many square brackets as there are

indicesndash Each index must be enclosed in its own brackets

double[][]table = new double[100][10]int[][][] figure = new int[10][20][30]Person[][] = new Person[10][100]

6-15Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Multidimensional Arrays

bull Multidimensional arrays may have any number of indices but perhaps the most common number is two

ndash Two-dimensional array can be visualized as a two-dimensional display with the first index giving the row and the second index giving the columnchar[][] a = new char[5][12]

ndash Note that like a one-dimensional array each element of a multidimensional array is just a variable of the base type (in this case char)

6-16Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Multidimensional Arraysbull In Java a two-dimensional array such as a is actually

an array of arrays

ndash The array a contains a reference to a one-dimensional array of size 5 with a base type of char[]

ndash Each indexed variable (a[0] a[1] etc) contains a reference to a one-dimensional array of size 12 also with a base type of char[]

bull A three-dimensional array is an array of arrays of arrays and so forth for higher dimensions

6-17Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Two-Dimensional Array as an Array of Arrays (Part 1 of 2)

6-18Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Question How to access every element in a two-dimensional array

bull char[][] a = new char[5][12]

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Two-Dimensional Array as an Array of Arrays (Part 2 of 2)

6-20Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Using the length Instance Variable

char[][] page = new char[30][100]

bull The instance variable length does not give the total number of indexed variables in a two-dimensional arrayndash Because a two-dimensional array is actually an array of arrays the

instance variable length gives the number of first indices (or rows) in the arraybull pagelength is equal to 30

ndash For the same reason the number of second indices (or columns) for a given row is given by referencing length for that row variablebull page[0]length is equal to 100

6-21Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Using the length Instance Variable

bull The following program demonstrates how a nested for loop can be used to process a two-dimensional arrayndash Note how each length instance variable is used

int row column

for (row = 0 row lt pagelength row++)

for (column = 0 column lt page[row]length

column++)

page[row][column] = Z

6-22Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Ragged Arrays

bull Each row in a two-dimensional array need not have the same number of elementsndash Different rows can have different numbers of

columnsbull An array that has a different number of

elements per row it is called a ragged array

6-23Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Create Ragged Arraysdouble[][] a = new double[3][5]

bull The above line is equivalent to the followingdouble [][] a

a = new double[3][] Note below

a[0] = new double[5]

a[1] = new double[5]

a[2] = new double[5]ndash Note that the second line makes a the name of an array with room for

3 entries each of which can be an array of doubles that can be of any length

ndash The next 3 lines each create an array of doubles of size 5

6-24Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Create Ragged Arrays

double [][] a

a = new double[3][]

bull Since the above line does not specify the size of a[0] a[1] or a[2] each could be made a different size insteada[0] = new double[5]

a[1] = new double[10]

a[2] = new double[4]

6-25Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Multidimensional Array Parameters and Returned Values

bull Methods may have multidimensional array parametersndash They are specified in a way similar to one-dimensional

arraysndash They use the same number of sets of square brackets as

they have dimensionspublic void myMethod(int[][] a)

ndash The parameter a is a two-dimensional array

6-26Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Multidimensional Array Parameters and Returned Values

bull Methods may have a multidimensional array type as their return typendash They use the same kind of type specification as for

a multidimensional array parameterpublic double[][] aMethod()

ndash The method aMethod returns an array of double

6-27Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

A Grade Book Classbull As an example of using arrays in a program a class GradeBook is used to process quiz scores

bull Objects of this class have three instance variablesndash grade a two-dimensional array that records the grade of

each student on each quizndash studentAverage an array used to record the average

quiz score for each studentndash quizAverage an array used to record the average

score for each quiz

6-28Copyright copy 2008 Pearson Addison-Wesley All rights reserved

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

A Grade Book Class

bull The score that student 1 received on quiz number 3 is recorded in grade[0][2]

bull The average quiz grade for student 2 is recorded in studentAverage[1]

bull The average score for quiz 3 is recorded in quizAverage[2]

bull Note the relationship between the three arrays

6-29Copyright copy 2008 Pearson Addison-Wesley All rights reserved

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

The Two-Dimensional Array grade

6-30Copyright copy 2008 Pearson Addison-Wesley All rights reserved

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Quiz

There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement

Announcement

bull Next Lecture Inheritance

bull Reading assignment Chapter 7

  • Slide 1
  • Programming tips Commentsdocumentations
  • Programming tips interaction with users
  • Review on arrays
  • Question Is an Array of Characters a String
  • An Array of Characters Is Not a String
  • Arguments for the Method main
  • Slide 8
  • Slide 9
  • Exercise Write a method that tests two integer arrays to see if they are equal (of same length amp contain the same integer values)
  • equalsArray
  • The Arrays class
  • Slide 13
  • Exercise There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from keyboard calculate the average score for each student and output the average scores from the highest to the lowest
  • Multidimensional Arrays
  • Slide 16
  • Slide 17
  • Two-Dimensional Array as an Array of Arrays (Part 1 of 2)
  • Question How to access every element in a two-dimensional array
  • Two-Dimensional Array as an Array of Arrays (Part 2 of 2)
  • Using the length Instance Variable
  • Slide 22
  • Ragged Arrays
  • Create Ragged Arrays
  • Slide 25
  • Multidimensional Array Parameters and Returned Values
  • Slide 27
  • A Grade Book Class
  • Slide 29
  • The Two-Dimensional Array grade
  • Quiz There are 5 registered students in our class Each takes 4 courses this semester Write a program that read scores from userrsquos input calculate the average score for each student and output the average scores from the highest to the lowest
  • Announcement