


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
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
In this lecture, students are introduced to multi-dimensional arrays and nested for loops in java. The lecture covers the concept of 2-dimensional arrays, their organization in memory, and how to access their elements using two indices. Students are also encouraged to think of real-life applications of 2-dimensional arrays, such as gameboards, spreadsheets, and maps. The lecture includes examples, exercises, and coding activities to help students understand the concepts.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
Reminders Program due Friday. Any questions about the current programming assignment? Quiz: Wednesday, March 29 More Nested For Loops Let's do one more triangle that is a bit more complicated - Try to write method that prints triangles like this:
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. Using loops to access the array elements In a 1D array, we use the array's length in a for loop to access each element of an array. [Give an example] What do you think anArray.length means? What is the value of anArray.length? [3] Let's say that we wanted to print the contents of a 2D array. How would we do it?
Lab Exercise Modify the TicTacToe game