Two-Dimensional Arrays
Take your understanding of arrays and loops to the next dimension! Learn how to create and use two-dimensional arrays.
StartKey Concepts
Review core concepts you need to learn to master this subject
Nested Iteration Statements
Declaring 2D Arrays
Accessing 2D Array Elements
Initializer Lists
Modify 2D Array Elements
Row-Major Order
Column-Major Order
Traversing With Enhanced For Loops
Nested Iteration Statements
Nested Iteration Statements
for(int outer = 0; outer < 3; outer++){
System.out.println("The outer index is: " + outer);
for(int inner = 0; inner < 4; inner++){
System.out.println("\tThe inner index is: " + inner);
}
}
In Java, nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue.
- 1As we have learned previously, an array is a group of data consisting of the same type. This means that we can have an array of primitive data types (such as integers): [1, 2, 3, 4, 5] We can ev…
- 2When declaring 2D arrays, the format is similar to normal, one-dimensional arrays, except that you include an extra set of brackets after the data type. In this example, int represents the data typ…
- 3Let’s first review how to access elements in regular arrays. For a normal array, all we need to provide is an index (starting at 0) which represents the position of the element we want to access. …
- 4Now let’s review how to modify elements in a normal array. For a one dimensional array, you provide the index of the element which you want to modify within a set of brackets next to the variable …
- 5We’re about to look at how we can use loops to make our lives easier when working with 2D arrays. But before we do that, let’s take a moment to refresh ourselves on how nested loops work. Nested l…
- 6In the last exercise, we reviewed how to use nested loops as well as how to iterate through regular arrays using loops. In this exercise, we will apply that knowledge in order to learn how to trave…
- 7We have seen how to traverse 2D arrays using standard for loops, but in this exercise, we will practice traversing them using some other loop types. For example, you may want to only retrieve e…
- 8Row-major order for 2D arrays refers to a traversal path which moves horizontally through each row starting at the first row and ending with the last. Although we have already looked at how 2D ar…
- 9Column-major order for 2D arrays refers to a traversal path which moves vertically down each column starting at the first column and ending with the last. This ordering system also conceptualizes…
- 10When working with 2D arrays, it is important to be able to combine traversal logic with conditional logic in order to effectively navigate and process the data. Here are a few ways in how condition…
- 11Let’s review the concepts we have learned throughout this lesson. Arrays are objects in Java, we can have arrays of objects, therefore we can also have arrays of arrays. This is the way 2D arrays …
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory