CSCE145 F2026 04 Arrays Part 03
Overview
Java multidimensional arrays are arrays of arrays: each dimension is indexed separately, and each inner array has its own length. The lesson applies these ideas in a 10×10 hide-and-seek game, using nested loops, character markers, random coordinates, movement validation, and squared-distance feedback, then introduces curly-brace initialization and ragged arrays.
Key takeaways
- A Java two-dimensional array is an outer array of references to inner arrays, so array.length and array[i].length can report different dimensions.
- Nested loops should use the current row’s length in the inner condition; this also makes traversal safe for ragged arrays.
- The hide-and-seek game maps grid coordinates to board[y][x], while keeping x and y as separate player and goal coordinates.
- Squared Euclidean distance can be compared against squared thresholds to classify proximity without calculating a square root.
- Validating dx and dy independently allows one coordinate change to be rejected without discarding a valid change on the other axis.
- Clamping both coordinates to 0 to 9 keeps movement within a 10×10 board, and a win requires both x and y to match the goal.
Chapters
- Declare a two-dimensional integer array with a type such as int[][] and create it with dimensions such as new int[2][3].
- The outer array stores references to inner arrays; each cell is accessed with two indices, starting at 0.
- array.length gives the outer dimension, while array[i].length gives the length of the inner array at index i.
- Use an outer loop for row index i and an inner loop for column index j; the example fills each cell with i + j.
- The inner loop condition should use array[i].length, not array.length, so it checks the current row’s size.
- For a 2×3 array, the example produces rows [0, 1, 2] and [1, 2, 3].
- The game places a player and a randomly selected hidden goal on a 10×10 character board.
- The player can move in eight directions; visited positions become path markers, and moves are kept inside the board.
- Distance feedback classifies the player as getting colder, warmer, or hotter, and reaching the goal ends the game.
- The program imports java.util.Scanner for console input and java.util.Random for selecting goal coordinates.
- Constants define a board size of 10, squared cold and warm thresholds, and character markers for empty spaces, the player, the path, and a debug-only goal.
- The player starts at (0, 0); Random.nextInt(10) selects valid goal coordinates from 0 through 9.
- A char[10][10] board is filled with underscores using nested loops, then the player and hidden goal are placed using board[y][x].
- Nested loops print each board row, using print for individual characters and println after each row.
- The game compares squared coordinate distance with squared thresholds, avoiding a square-root calculation.
- A distance above the cold threshold prints “getting colder”; otherwise, a distance above the warm threshold prints “getting warmer,” and closer positions print “getting hotter.”
- Scanner reads dx and dy, each restricted to -1, 0, or 1; separate if statements validate the two independent inputs.
- The current cell becomes a path marker before px and py are updated by their respective deltas.
- Coordinate checks clamp x and y to the valid range 0 to 9, preventing the player from leaving the board.
- The game ends when both player coordinates match the goal coordinates; the demonstrated run reaches the hidden goal and prints a win message.
- Curly-brace initializers can provide all values directly, with nested braces representing the inner arrays.
- Java supports ragged arrays whose rows have different lengths; the example has three rows containing 5, 8, and 2 elements.
- Because row lengths can vary, traverse each row using a[i].length rather than assuming every inner array has the same size.
Transcript excerpt
0:00 Hey everyone, how goes it? And welcome back to computer science 145. Today we're going to continue to discuss arrays. As mentioned in a previous lecture, an array is a collection of variables of the same type. This is a foundational data structure and it is a contiguous block of memory. Furthering this concept, arrays can have multiple dimensions. The way that we can accomplish this is by simply using more of the square brackets. In this class, we're only going to focus on two-dimensional arrays, but you can have more dimensions as you see fit. In order to create a two-dimensional
0:48 array, you do it in a similar way. First, you use the type followed by two square brackets and then an identifier. After that we construct this by using the type and then within the two square brackets you put the two different sizes of this array. If we needed to create a 2x3 array of integers then we just use the type int two square brackets the identifier then new int followed by two and three. Now, as Java considers arrays to be these container objects, whenever we create multi-dimensional arrays, you can think of this as being an array of other arrays. Always keep in mind that the identifier
1:39 of an array references a memory address. This memory address references the contents of the array. This means that you might have an array that simply has memory addresses to other arrays. Looking at this in memory, we can consider it like this. Following this statement, if we arrive right here, we understand that this declares the array. So over here we have the identifier array and currently its contents is null because remember this is going to contain the memory address to the contents in the array.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, UofSC_CSCE145_CSCE146.