Fiveable
Fiveable

or

Log in

Find what you need to study


Light

Find what you need to study

Unit 8 Overview: 2D Array

5 min readnovember 15, 2020

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway From this Unit

Unit Overview

Exam Weighting

  • 7.5-10% of the test

  • Roughly 3 to 4

  • Always , which tests your ability to make, traverse, and create algorithms with a 2D .

Enduring Understanding

In the past two units, you've learned how to store data in an or an . Now it's time to take it to another dimension with . You can either think of them as a or as a , and we'll use both methods of thinking in this unit. Using these, we'll learn how to set these up and also how to traverse them.

Building Computational Thinking

For this unit, we will first learn how to create and with various types of objects. These will be nested arrays. When traversing a list, you need to use nested arrays for loops, and these will need to have proper bounds to avoid an . If you can master , you can easily do 3D and higher-dimensional arrays if you ever decide to continue using Java after this course!

Main Ideas for This Unit

  • 2D Algorithms

8.1: 2D Arrays

Initializing 2D Arrays

We can declare and initialize a 2D in much the same form as a regular , but with some subtle differences. However, there are still two ways to do so. We can initialize an empty 2D that fills the with the same "null"/initialized values as when we use the regular arrays from Unit 6 (also called 1D arrays). However, this only works for "rectangular" arrays, where the two dimensions are clearly defined. We will talk more about this in the next subsection.

Here is how to do this:

/* Template: type[][] twoDArrayName = new type[firstDimension][secondDimension] */ int[][] arrayA = new int[3][4];

You can also initialize this with a pre-existing 2D . This is similar to how you do 1D arrays.

Here is an example of an int[3][4] 2D but with values pre-initialized:

int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

You don't have to use integers for the type stored in the . You can use any other primitive or reference type! Integers are only used in this guide for simplicity.

Representing 2D Arrays

The can be represented in 2 major ways: one that is more visual, and one that is closer to how it is stored in the memory.

Memory Representation

The first way to think about this is how it is stored in memory, or in terms of data structures. A 2D in this form is just a . There is an having arrays for its individual terms. In each , the terms are objects or of the type stated during initialization. When we initialize an of type[firstDimension][secondDimension], we are actually initializing an of firstDimension (remember to use instead of size as size is only for ArrayLists as in Unit 7). Each item in this is another containing secondDimension items of the specified type. For example, arrayA is an with 3 items, each of which is an with 4 integers.

A 2D does not have to be rectangular. The inner arrays can vary in size. Here is an example of a :

int[][] arrayC = {{1, 2}, {3}, {4, 5, 6}};

However, for the rest of this unit, we will be concerned with rectangular arrays, as those will be the type tested on the AP Exam.

Graphical Representation (only works for rectangular arrays)

For rectangular arrays, we can think of these as a , table, or matrix (if you have learned them in a prior math class). We can express arrayB from earlier as follows:

int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

For a 2D , type [firstDimension][secondDimension] . In this representation, firstDimension is the number of and secondDimension is the number of columns. Remember that arrayB is an example of an int[3][4] 2D . Using the same logic, arrayB has 3 and 4 columns.

From this visual idea, we can easily find the indices to access an individual element of a 2D .

Indices for Elements in an int[3][4] Array

[0][0][0][1][0][2][0][3]
[1][0][1][1][1][2][1][3]
[2][0][2][1][2][2][2][3]

For indices, we use 2 bracketed numbers, called , with the first being the row and the second being the column. Remember that Java is a , so the row indices range from [0, 1, 2, ..., firstDimension - 1] and the column indices range from [0, 1, 2, ..., firstDimension - 1].

For a pre-initialized , the number of is the of the , which is arrayName., while the number of columns is the of each , which for rectangular arrays is arrayName[0]..

With , we can access any element of the . However, if you use an index that is not in the allowed range, you get an . For example, for arrayB, saying arrayB[2][3] means that we want the element in the third row and the fourth column, which corresponds to 12. Lets do a little practice using arrayB from above:

What are the indices for the following: 1. 6 2. 4 3. 9 4. 5 What values are given for the following indices: 5. [1][3] 6. [0][2] 7. [3][2] 8. [2][0]

Answers

1. [1][1] (2nd row, 2nd column) 2. [0][3] (1st row, 4th column) 3. [2][0] (3rd row, 1st column) 4. [1][0] (2nd row, 1st column) 5. 8 (2nd row, 4th column) 6. 3 (1st row, 3rd column) 7. The last row in the has row index 2, since there is no fourth row with index 3, an is thrown 8. 9 (3rd row, 1st column)

We will use the of in the rest of this unit as it is easier to visualize and understand.

Key Terms to Review (29)

0-indexed language

: A 0-indexed language is a programming language where arrays and other data structures start counting from 0 instead of 1. In these languages, the first element is accessed with an index of 0.

2D Arrays

: 2D arrays are data structures that store values in a grid-like format with rows and columns. They allow for the organization and manipulation of data in a two-dimensional manner.

Array

: An array is a fixed-size collection of elements of the same type stored in contiguous memory locations. It allows efficient access to individual elements using an index.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

ArrayList

: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Building Computational Thinking

: Building computational thinking refers to developing problem-solving skills and logical reasoning abilities through programming and computer science concepts.

Double-index notation

: Double-index notation is a way of representing elements in a two-dimensional array using two indices, one for the row and one for the column. It allows easy access to specific elements within the array.

Enduring Understanding

: Enduring Understanding refers to a deep and lasting comprehension of a concept or idea in computer science. It goes beyond memorization and allows students to apply their knowledge in various contexts.

Exam Weighting

: Exam weighting refers to the allocation of points or marks given to each section or topic in an exam. It determines how much each part contributes to the overall score.

FRQ #4

: FRQ #4 refers to the fourth free-response question in the AP Computer Science A exam. It is a coding problem that requires students to write a program to solve a specific task or problem.

Graphical Representation

: Graphical representation involves using visual elements such as charts, graphs, or diagrams to present information or data visually.

Grid

: A grid is a two-dimensional arrangement of cells or elements organized in rows and columns.

Initialize 2D Arrays

: Initializing 2D arrays means setting up or creating a two-dimensional array with initial values before using it in a program.

Initializing 2D Arrays

: Initializing 2D arrays refers to the process of declaring and assigning values to a two-dimensional array. It involves specifying the size of the array and providing initial values for each element.

Inner Array

: An inner array refers to an array that is contained within another array.

Key Term: Practice with Indices and Values

: Definition: This term refers to the process of working with indices and values in computer programming. Indices are used to access specific elements within a data structure, such as an array or a string, while values represent the actual data stored at those indices.

Length

: The length refers to the number of elements in an array or the size of a string.

Main Ideas for This Unit

: The main ideas for this unit refer to the key concepts and topics that students need to understand and master in order to successfully complete the unit. These ideas serve as a roadmap for learning and provide a framework for organizing knowledge.

Manipulate

: Manipulate refers to the process of changing or altering data in a program. It involves modifying variables, arrays, or objects to achieve a desired outcome.

Multiple-choice Questions

: Multiple-choice questions are a type of assessment where students choose the correct answer from a set of options. Each question typically has one correct answer and several distractors.

Nested Array

: A nested array is an array that contains other arrays as its elements. It allows for the creation of multi-dimensional data structures.

Nested For Loops

: Nested for loops are a way to repeat a set of instructions multiple times within another loop. It allows you to iterate over elements in a multi-dimensional data structure, such as a 2D array.

Non-rectangular Array

: A non-rectangular array is an array where each row can have a different number of columns.

Outer Array

: An outer array is an array that contains other arrays as its elements.

Primitive Type Values

: Primitive type values refer to basic data types in programming languages such as integers, floating-point numbers, characters, booleans, etc., which are not composed of smaller parts.

Representations of 2D Arrays

: Representations of 2D arrays refer to different ways in which two-dimensional arrays can be visualized or stored in memory. There are multiple representations available, such as using nested loops, using matrices, or using lists of lists.

Representing 2D Arrays

: Representing 2D arrays refers to the process of storing and accessing data in a two-dimensional grid-like structure. It allows for efficient manipulation and organization of data that has both rows and columns.

Rows

: Rows refer to the horizontal lines in a table or grid that contain data. They represent individual records or entries in a dataset.

Traversing 2D Arrays

: Traversing 2D arrays refers to the process of accessing and examining each element in a two-dimensional array. It involves iterating through the rows and columns of the array to perform operations on each element.

Unit 8 Overview: 2D Array

5 min readnovember 15, 2020

Athena_Codes

Athena_Codes

Athena_Codes

Athena_Codes

The Big Takeaway From this Unit

Unit Overview

Exam Weighting

  • 7.5-10% of the test

  • Roughly 3 to 4

  • Always , which tests your ability to make, traverse, and create algorithms with a 2D .

Enduring Understanding

In the past two units, you've learned how to store data in an or an . Now it's time to take it to another dimension with . You can either think of them as a or as a , and we'll use both methods of thinking in this unit. Using these, we'll learn how to set these up and also how to traverse them.

Building Computational Thinking

For this unit, we will first learn how to create and with various types of objects. These will be nested arrays. When traversing a list, you need to use nested arrays for loops, and these will need to have proper bounds to avoid an . If you can master , you can easily do 3D and higher-dimensional arrays if you ever decide to continue using Java after this course!

Main Ideas for This Unit

  • 2D Algorithms

8.1: 2D Arrays

Initializing 2D Arrays

We can declare and initialize a 2D in much the same form as a regular , but with some subtle differences. However, there are still two ways to do so. We can initialize an empty 2D that fills the with the same "null"/initialized values as when we use the regular arrays from Unit 6 (also called 1D arrays). However, this only works for "rectangular" arrays, where the two dimensions are clearly defined. We will talk more about this in the next subsection.

Here is how to do this:

/* Template: type[][] twoDArrayName = new type[firstDimension][secondDimension] */ int[][] arrayA = new int[3][4];

You can also initialize this with a pre-existing 2D . This is similar to how you do 1D arrays.

Here is an example of an int[3][4] 2D but with values pre-initialized:

int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

You don't have to use integers for the type stored in the . You can use any other primitive or reference type! Integers are only used in this guide for simplicity.

Representing 2D Arrays

The can be represented in 2 major ways: one that is more visual, and one that is closer to how it is stored in the memory.

Memory Representation

The first way to think about this is how it is stored in memory, or in terms of data structures. A 2D in this form is just a . There is an having arrays for its individual terms. In each , the terms are objects or of the type stated during initialization. When we initialize an of type[firstDimension][secondDimension], we are actually initializing an of firstDimension (remember to use instead of size as size is only for ArrayLists as in Unit 7). Each item in this is another containing secondDimension items of the specified type. For example, arrayA is an with 3 items, each of which is an with 4 integers.

A 2D does not have to be rectangular. The inner arrays can vary in size. Here is an example of a :

int[][] arrayC = {{1, 2}, {3}, {4, 5, 6}};

However, for the rest of this unit, we will be concerned with rectangular arrays, as those will be the type tested on the AP Exam.

Graphical Representation (only works for rectangular arrays)

For rectangular arrays, we can think of these as a , table, or matrix (if you have learned them in a prior math class). We can express arrayB from earlier as follows:

int[][] arrayB = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

For a 2D , type [firstDimension][secondDimension] . In this representation, firstDimension is the number of and secondDimension is the number of columns. Remember that arrayB is an example of an int[3][4] 2D . Using the same logic, arrayB has 3 and 4 columns.

From this visual idea, we can easily find the indices to access an individual element of a 2D .

Indices for Elements in an int[3][4] Array

[0][0][0][1][0][2][0][3]
[1][0][1][1][1][2][1][3]
[2][0][2][1][2][2][2][3]

For indices, we use 2 bracketed numbers, called , with the first being the row and the second being the column. Remember that Java is a , so the row indices range from [0, 1, 2, ..., firstDimension - 1] and the column indices range from [0, 1, 2, ..., firstDimension - 1].

For a pre-initialized , the number of is the of the , which is arrayName., while the number of columns is the of each , which for rectangular arrays is arrayName[0]..

With , we can access any element of the . However, if you use an index that is not in the allowed range, you get an . For example, for arrayB, saying arrayB[2][3] means that we want the element in the third row and the fourth column, which corresponds to 12. Lets do a little practice using arrayB from above:

What are the indices for the following: 1. 6 2. 4 3. 9 4. 5 What values are given for the following indices: 5. [1][3] 6. [0][2] 7. [3][2] 8. [2][0]

Answers

1. [1][1] (2nd row, 2nd column) 2. [0][3] (1st row, 4th column) 3. [2][0] (3rd row, 1st column) 4. [1][0] (2nd row, 1st column) 5. 8 (2nd row, 4th column) 6. 3 (1st row, 3rd column) 7. The last row in the has row index 2, since there is no fourth row with index 3, an is thrown 8. 9 (3rd row, 1st column)

We will use the of in the rest of this unit as it is easier to visualize and understand.

Key Terms to Review (29)

0-indexed language

: A 0-indexed language is a programming language where arrays and other data structures start counting from 0 instead of 1. In these languages, the first element is accessed with an index of 0.

2D Arrays

: 2D arrays are data structures that store values in a grid-like format with rows and columns. They allow for the organization and manipulation of data in a two-dimensional manner.

Array

: An array is a fixed-size collection of elements of the same type stored in contiguous memory locations. It allows efficient access to individual elements using an index.

ArrayIndexOutOfBoundsException

: ArrayIndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position in an array. It is thrown to indicate that the index used to access an array is either negative or greater than or equal to the size of the array.

ArrayList

: ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.

Building Computational Thinking

: Building computational thinking refers to developing problem-solving skills and logical reasoning abilities through programming and computer science concepts.

Double-index notation

: Double-index notation is a way of representing elements in a two-dimensional array using two indices, one for the row and one for the column. It allows easy access to specific elements within the array.

Enduring Understanding

: Enduring Understanding refers to a deep and lasting comprehension of a concept or idea in computer science. It goes beyond memorization and allows students to apply their knowledge in various contexts.

Exam Weighting

: Exam weighting refers to the allocation of points or marks given to each section or topic in an exam. It determines how much each part contributes to the overall score.

FRQ #4

: FRQ #4 refers to the fourth free-response question in the AP Computer Science A exam. It is a coding problem that requires students to write a program to solve a specific task or problem.

Graphical Representation

: Graphical representation involves using visual elements such as charts, graphs, or diagrams to present information or data visually.

Grid

: A grid is a two-dimensional arrangement of cells or elements organized in rows and columns.

Initialize 2D Arrays

: Initializing 2D arrays means setting up or creating a two-dimensional array with initial values before using it in a program.

Initializing 2D Arrays

: Initializing 2D arrays refers to the process of declaring and assigning values to a two-dimensional array. It involves specifying the size of the array and providing initial values for each element.

Inner Array

: An inner array refers to an array that is contained within another array.

Key Term: Practice with Indices and Values

: Definition: This term refers to the process of working with indices and values in computer programming. Indices are used to access specific elements within a data structure, such as an array or a string, while values represent the actual data stored at those indices.

Length

: The length refers to the number of elements in an array or the size of a string.

Main Ideas for This Unit

: The main ideas for this unit refer to the key concepts and topics that students need to understand and master in order to successfully complete the unit. These ideas serve as a roadmap for learning and provide a framework for organizing knowledge.

Manipulate

: Manipulate refers to the process of changing or altering data in a program. It involves modifying variables, arrays, or objects to achieve a desired outcome.

Multiple-choice Questions

: Multiple-choice questions are a type of assessment where students choose the correct answer from a set of options. Each question typically has one correct answer and several distractors.

Nested Array

: A nested array is an array that contains other arrays as its elements. It allows for the creation of multi-dimensional data structures.

Nested For Loops

: Nested for loops are a way to repeat a set of instructions multiple times within another loop. It allows you to iterate over elements in a multi-dimensional data structure, such as a 2D array.

Non-rectangular Array

: A non-rectangular array is an array where each row can have a different number of columns.

Outer Array

: An outer array is an array that contains other arrays as its elements.

Primitive Type Values

: Primitive type values refer to basic data types in programming languages such as integers, floating-point numbers, characters, booleans, etc., which are not composed of smaller parts.

Representations of 2D Arrays

: Representations of 2D arrays refer to different ways in which two-dimensional arrays can be visualized or stored in memory. There are multiple representations available, such as using nested loops, using matrices, or using lists of lists.

Representing 2D Arrays

: Representing 2D arrays refers to the process of storing and accessing data in a two-dimensional grid-like structure. It allows for efficient manipulation and organization of data that has both rows and columns.

Rows

: Rows refer to the horizontal lines in a table or grid that contain data. They represent individual records or entries in a dataset.

Traversing 2D Arrays

: Traversing 2D arrays refers to the process of accessing and examining each element in a two-dimensional array. It involves iterating through the rows and columns of the array to perform operations on each element.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.