Professional Documents
Culture Documents
Review
If the == operator has two array variable operands, what is being
compared?
The reference variables held in the variables.
How do you compare two arrays to see if they hold the same
elements?
Iterate through the arrays and compare them element by
element.
How are the sum and average array algorithms similar? Both iterate through the array and add each element to an
accumulator variable.
How are the minimum and maximum element algorithms similar? The current minimum or maximum element is compared to each
element, if an element is smaller (minimum case) or larger (maximum case) than the current minimum or maximum, the element replaces it.
If you intend to use a partially filled array, what information must
students.
How would we represent this in a program with what
weve learned?
30-element arrays. This was the problem arrays were supposed to solve!
considered one dimensional arrays, because they only extend along one dimension (one set of data). Java also allows for higher dimensional arrays, for example, two dimensional arrays.
Two dimensional arrays can be though of having a table
array!
rows and 4 columns called numbers you would: int[][] numbers = new int[3][4];
The two sets of brackets on the left side of the assignment operator indicates the variable is a reference variable to a two dimensional array. The [3] on the right side indicates the array object will have 3 rows. The [4] on the right side indicates the array object will have 4 columns.
third column of the above declared numbers array you would: numbers[0][2]
brackets, separating elements by commas. You separate each row by commas and put the entire array in curly brackets:
int[][] numbers = {{1,2,3}, {4,5,6}, {7,8,9}}; or int[][] numbers = {{1,2,3}, {4,5,6}, {7,8,9}};
Another way to think of two dimensional arrays is that a two dimensional
int[][] numbers = new int[3][4]; numbers.length the number of rows numbers[0].length the number of columns in the first row
Example: TwoDimLengths.java
students scores on 3 quizzes. Then, the program should tell the user the average for each student, each quiz, and overall class average for all quizzes.
Ragged Arrays
Since the rows of a two dimensional array are
arrays themselves, they can have different lengths: When the rows of a two dimensional array are of different lengths, the array is known as a ragged array.
int[][] ragged = new int[4][]; ragged[0] = new int[3]; ragged[1] = new int[4]; ragged[2] = new int[5]; ragged[3] = new int[6]; Example: RaggedArray.java
dimensions an array can be. For instance we can create a three dimensional array: int[][][] numbers = new int[2][3][4];
You can have higher dimensional arrays by simply
dimensions.
For instance you can have a three dimensional