Docsity

2D ARRAY ACTIVITY Computer Programming, Quizzes of Computer Programming

Partial preview of the text

Download 2D ARRAY ACTIVITY Computer Programming and more Quizzes Computer Programming in PDF only on Docsity!

Two-Dimensional Arrays

15-110 Summer 2010

Margaret Reid-Miller

Two-Dimensional Arrays

  • Arrays that we have consider up to now are one-

dimensional arrays, a single line of elements.

  • Often data come naturally in the form of a table, e.g.,

spreadsheet, which need a two-dimensional array.

  • Examples:
    • Lab book of multiple readings over several days
    • Periodic table
    • Movie ratings by multiple reviewers.
      • Each row is a different reviewer
      • Each column is a different movie

Similarity with 1D Arrays

  • Each element in the 2D array must by the same type,
    • either a primitive type or object type
  • Subscripted variables can be use just like a variable: ! rating[0][3] = 10;!
  • Array indices must be of type int and can be a

literal, variable, or expression.

rating[3][j] = j;!

  • If an array element does not exists, the Java runtime

system will give you an

!! ArrayIndexOutOfBoundsException

Declaring 2D Arrays

  • Declare a local variable rating that references a 2D

array of int:

  • Declare a field family that reference a 2D array of

GiftCards:

  • Create a 2D array with 3 rows and 4 columns and

assign the reference to the new array to rating:

  • Shortcut to declare and create a 2D array: int[][] rating; private GiftCard[][] family;! rating = new int[3][4];! int[][] rating = new int[3][4];

Size of 2D Arrays

  • When you write a method that has a 2D array as a

parameter, how do you determine the size of the

array?

Hint:

  • Consider a variable words, a 1D

array of String references.

  • What is the length of the array?
  • What is the length of the word at

index 2?

words!

this!

that!

what!

0 1 2

2D Array Implementation

  • A 2D array is a 1D array of (references to) 1D arrays.

int[][] rating = new int[3][4];!

Example 2

  • Find the number of ratings above the value of the

parameter.

public int countAbove(int[][] rating, int num) {! int count = 0;! for (int row = 0; row < ; row++) {! for (int col = 0; col < ; col++) {! ! if (rating[row][col] > num)! count++;! }! }! return count;! }! number of rows number of columns rating.length! rating[0].length!

Example 3

• Print the average rating for

the movie in column 3.

int sum = 0;! for (int row = 0; row < ______________ ; row++) {! ! sum += rating[_____][____];! }! System.out.println((double) sum / ______________ );! rating.length row 3! rating.length

movie reviewer

Example 3 Revisited

  • Print the average rating for the movie in column 3. int count = 0;! double sum = 0;! for (int row = 0; row < rating.length; row++) {! ! if (_____________________ > 3) {! sum += rating[_____][_____];! count++;! }! }! if (count > 0) {! System.out.println((double) sum / count);! }!

rating[row].length row 3!

2D Array of Object References

• Recall that creating an array of object

references fills the array with null values.

• Example:

GiftCard[][] family = new GiftCard[3][4]!

null

Example 4

  • Print the total value of the gift cards for each family

member (rows): printValueOfRows(family);!

public static void printValueOfRows(______________ data) {! for (int row = 0; row < __________________; row++) {! double total = ___________; // find total for the row! for (int col = 0; col < ___________________; col++) {! if (data[row][col] != ___________) {! total += data[row][col].getBalance();! }! }! System.out.println(“Row “ + row + “: $“ + total);! }! }! data.length data[row].length null

GiftCard[][]!