Download presentation

Presentation is loading. Please wait.

Published byAinsley Weary Modified over 3 years ago

1
CS0007: Introduction to Computer Programming Arrays: Higher Dimensional Arrays

2
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 be kept in addition to the array itself? The current number of elements in the array How does the Sequential Search Algorithm work? Look at every element in the array starting at the first element and check if it is the target value.

3
Two Dimensional Arrays Imagine now we want to keep 10 test scores for 30 students. How would we represent this in a program with what we’ve learned? Answer: You would need either 30 10-element arrays or 10 30-element arrays. This was the problem arrays were supposed to solve! What we have been working with so far are 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 of rows and columns in memory. So, a better solution to the above question is what? Answer: Have a 30 x 10 or a 10 x 30 two dimensional array!

4
Two Dimensional Array Example New Topics: Two Dimensional Arrays

5
Declaring a two dimensional array is very similar to declaring a one dimensional array. For example, to declare an integer array variable with 3 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. Each array element has two indices, one indicating the row and one the column. For example, to access the element in the first row and third column of the above declared numbers array you would: numbers[0][2]

6
Two Dimensional Arrays To initialize a two dimensional array you encapsulate each row in curly 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 is a one dimensional array of one dimensional arrays. Said another way, it is a reference variable to an array that holds reference variables to arrays. This is apparent when using the length data member in two dimensional arrays: 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

7
Class Programming Practice: ClassScores.java Create a program where a user can enter 3 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.

8
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

9
Higher Dimensional Arrays Java does not impose a limit on how many 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 defining the size of the dimension. You can also have ragged arrays of higher dimensions. For instance you can have a three dimensional array with 3 two- dimensional arrays of sizes 3x3, 4x5, and 2x6.

Similar presentations

Presentation is loading. Please wait....

OK

Arrays.

Arrays.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on rational numbers for class 7 Convert a pdf to ppt online Ppt on human body digestive system Ppt on communication skills for teachers Download ppt on 1g 2g 3g 4g technology Ppt on fibonacci numbers list Ppt on search engine optimisation Download ppt on fundamental rights and duties of filipinos Ppt on gestational diabetes mellitus Ppt on mars exploration rover