


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of multi-dimensional arrays, focusing on 2-dimensional arrays and their application to image representation using chromakey technique. The lecture covers the definition, accessing elements, organization, and nested for loops for manipulating pixels in a photo. It also explains the difference between rows and columns and the length of a 2d array.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Next Programming Assignment I call it "virtual vacation". Discuss the assignment. This technique is called chromakey. Take everyone's picture. Question: how do we represent the image? We can use a 2-dimensional array. Multi-dimensional arrays We have looked at one-dimensional arrays. Java also has multidimensional arrays. int[] quizScores = new int[20]; ChessPiece[][] chessboard = new ChessPiece[8][8]; double [][][] temperature = new double[500][500][100]; We're going to look at 2-dimensional arrays. They are the most commonly used type of multi-dimensional array. A typical way of thinking about 2D arrays is as a set of rows and columns. The chessboard example has 8 rows and 8 columns. Other examples String[][] words = new String[10][4]; double[][] measurements = new double[3][5]; First dimension is the number of rows, second dimension is number of columns. Can anyone think of other 2-dimensional items that we could model using a 2D array?
To access an element of a 2D array, you need to use two indices. words[1][3] measurements[0][2] Let's look at this array: int[][] anArray = new int[3][4]; Draw picture of array elements in rows and columns. Identify each element by its indices. Stress that this is not how the array is actually laid out in memory. So, we can access or assign values to any element using its indices anArray[2][2] = 7; Array Organization The row and column model is a nice way for us to think about arrays – however, they are not stored in memory that way. Let's look at what is really going on. Let's look at our earlier declaration: int[][] anArray = new int[3][4]; What is anArray? It's a reference [Draw pic]. But what is it a reference to? Well, it is an array type. How many elements does it have? What is the type of each element? [Draw pic] anArray is a reference to an array with 3 elements. Each element is in turn an array of int. That is, each element in turn references an array with 4 elements that are each ints. [Draw indices in the pic] So, when you reference an array element such as anArray[1][2], you are really referencing anArray[1] and then element 2 of that array. Discuss rows and columns – the individual arrays correspond to the rows. A column is one element in each of the rows. Nested for loops We used loops to access every element of a 1-dimensional array. How are we going to access every element of a 2D array? Use nested loops.
What do you think .length means with a 2D array? What is the value of anArray.length? [3]. Remember, a 2D array is really an array of arrays. So, the length of the 2D array is its first dimension. Each of those arrays in turn has a length. How do we get the length of the first row? How about the second row? We can modify the above code like this: for ( int row = 0; row < picture.length; row++ ) { for ( int col = 0; col < picture[row].length; col++ )