Download presentation
Presentation is loading. Please wait.
1
Two-Dimensional Arrays
The arrays we’ve looked at so far have all been one-dimensional arrays because they store a simple list of values. A two-dimensional array has values in two “dimensions” which are like the rows and columns of a table or a grid. Many board games are based on a grid: checkers, chess, Scrabble, etc. Many apps are based on grids too: CandyCrush, 2048, Ruzzle. You can probably think of others. Two-dimensional arrays are a very common topic on the free response section of the AP test.
2
2D Arrays A one-dimensional array stores a list of elements
A two-dimensional array can be thought of as a table of elements, with rows and columns one dimension two dimensions
3
Two-Dimensional Arrays
To be precise, a two-dimensional array in Java is an array of arrays (row major) A two-dimensional array is declared by specifying the size of each dimension separately: int[][] scores = new int[12][50]; A two-dimensional array element is referenced using two index values value = scores[3][6] The array stored in one row or column can be specified using one index We must use two indexes to refer to a value in a two-dimensional array, one for the row and another for the column.
4
Two-Dimensional Arrays
Expression Type Description scores int[][] 2D array of integers, or array of integer arrays scores[5] int[] array of integers scores[5][12] int integer When you know the values that you want to store in the 2-D array, you can declare the array and store the values in it immediately. This operation is just like what you do for a one-dimensional array. Two pairs of brackets, are used to tell the computer that it is not a regular variable. Every cell in a 2-D array is assigned a pair of coordinates that are based on the row number and the column number. The first pair of brackets represents the row number and the second pair of brackets represents the column number. The rows and columns both begin at zero and end with one less than the number of rows or columns.
5
TwoDArray.java The TwoDArray program instantiates a two-dimensional array of integers. The size of the dimensions is specified when the array is created. In this case the array is 5 rows by 10 columns. Nested for loops are used to led the array with values and to print them in a table. Carefully trace the processing to see how the nested loops visit each elements in the two-dimensional array. Note that the outer loops are governed by table.length, which is the number of rows, and the inner loops are governed by table[row].length, which is the number of columns in that row. Please pause the video if you need more time to study the code.
6
TwoDArray Teminal Window
Here is the output of the two-dimensional array. The combination of print and “\t” allows the printout to be in multiple columns.
7
SodaSurvey.java As with one-dimensional arrays, an initializer list can be used to instantiate a two-dimensional array, where each element is itself an array initializer list. This technique is used in the SodaSurvey program shown here. Suppose a soda company held a teste test for four new flavors to see how teens liked them. The company got 10 students to try each new flavor and give it a score from 1 to 5,, where 1 means “gross” and 5 means “awesome.” The 2-d array called scores in the SodaSurvey program stores the results of that survey. Each row is a soda and each column in that row is the student who tasted it. More generally, each row holds the responses that all students gave or one particular soda flavor, and each column holds the responses of one student for all soda. The SodaSurvey program computes and prints the average responses for each soda and each student. The sums of each soda and student are first stored in 1-D arrays of integers. Then the averages are computed and printed. Remember that although the data is integers, the for loops used for the math are casting one of the integers to a double so a double can be outputted. Please pause the video if you need more time to study the code.
8
SodaSurvey Terminal Window
This what the output of the SodaSurvey program looks like.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.