8.2 Traversing 2D Arrays
Enduring Understanding
To manage large amounts of data or complex relationships in data, programmers write code that groups the data together into a single data structure without creating individual variables for each value.
Learning Objective
For 2D array objects:
Traverse using nested for loops.
Traverse using nested enhanced for loops.
Essential Knowledge
Nested iteration statements are used to traverse and access all elements in a 2D array. Since 2D arrays are stored as arrays of arrays, the way 2D arrays are traversed using for loops and enhanced for loops is similar to 1D array objects.
Nested iteration statements can be written to traverse the 2D array in “row-major order” or “column-major order.”
The outer loop of a nested enhanced for loop used to traverse a 2D array traverses the rows. Therefore, the enhanced for loop variable must be the type of each row, which is a 1D array. The inner loop traverses a single row. Therefore, the inner enhanced for
loop variable must be the same type as the elements stored in the 1D array.
Enduring Understanding
Programmers incorporate iteration and selection into code as a way of providing instructions for the computer to process each of the many possible input values.
Learning Objective
For algorithms in the context of a particular specification that requires the use of 2D array traversals:
§ Identify standard algorithms.
§ Modify standard algorithms.
§ Develop an algorithm.
Essential Knowledge
When applying sequential/linear search algorithms to 2D arrays, each row must be accessed then sequential/linear search applied to each row of a 2D array.
All standard 1D array algorithms can be applied to 2D array objects.
Last updated
Was this helpful?