Double Dimensional Arrays Flashcards
What is a 2D Array?
It is an array of arrays and it stores data in rows and columns (tabular format).
Elements can be accessed through using two indices which is?
row index and column index.
What is the other name of 2D dimensional?
multidimensional arrays
What is a data structure commonly used in mathematics to represent matrices and complex data structures?
2D Array
When a 2D Array is created, Java first creates a 1D array that contains _________ (pointers) to other 1D arrays, which represent the ___ of the 2D array.
references, rows
How are the rows of a 2D array stored in Java?
Each row in a 2D array is stored as a separate 1D array, and the main array contains pointers to these individual row arrays.
How many indices are required to access an element in a 2D array in Java?
A 2D array in Java requires two indices:
* One for the row
* One for the column
How do you initialize a 2D array in Java with specific values?
A 2D array can be initialized using curly braces {} with nested arrays:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
How do you declare a 2D array in Java?
Declared using the following syntax:
dataType[][] arrayName;
What are the ways to initialize a 2D Array
- Static Initialization
- Dynamic Initialization
Set the size first, then assign values later
Dynamic Initialization
Assign values directly during declaration.
Static Initialization
What happens when only the number of rows is specified while declaring a 2D array in Java?
The columns remain uninitialized, creating a jagged array where each row can have a different number of columns.
A ____ array is useful for storing ____________ , such as words of different lengths, lists of varying sizes, or ____________.
jagged, flexible data structures, irregularly shaped matrices
Why must the number of rows be specified first when creating a 2D array in Java?
In Java, a 2D array is an array of 1D arrays, so the number of rows must be specified first to allocate space for row references before defining column sizes.
What happens when a 2D array is declared without initializing rows or columns in Java?
It only creates a reference without allocating memory. The array must be initialized before use.
How do you declare a 2D array in Java without initializing its size?
You can declare a 2D array like this:
int[][] matrix;
This is capable of storing images, text, and other types of data, and is useful for data analysis and visualization?
2D Array
Specify the number of rows but leave columns uninitialized.
Double-Dimensional Array with Only Rows Initialized
What is a jagged Array?
is a 2D array where each row can have a different number of columns.
You must define rows first because a 2D array is an array of arrays.
Double-Dimensional Array with Only Columns Initialized
This type of array can declare a 2D array without initializing rows or columns.
Double-Dimensional Array with No Rows or Columns Initialized
This is just a reference; no memory is allocated yet and must be initialize before use.
Double-Dimensional Array with No Rows or Columns Initialized
This type of array can declare a 2D array without initializing rows or columns.
Double-Dimensional Array with No Rows or Columns Initialized
Where do 2D arrays are usually stored?
Often stored in memory as 1D arrays.
What are the 2 common methods for storing 2D array data?
*Row-Major Order.
*Column-Major Order.
How does Row-Major Order work?
All elements of the first row are stored consecutively. Followed by all elements of the second row, and so on.
What type languages that uses Row-Major Order?
Used in languages like C, C++, and Java.
In Row-Major Order, Elements are stored ______ by _____ in memory.
row by row
How does Column-Major Order work?
All elements of the first column are stored consecutively. Followed by all elements of the second column, and so on.
In Column-Major Order, Elements are stored ______ by _____ in memory.
column by column
What type languages that uses Column-Major Order?
Used in languages like Fortran, MATLAB, and R.
What are the key major differences between Row-Major and Column-Major?
Row-Major fills rows first, while Column-Major fills columns first
Arrays are stored in contiguous ________ ________.
memory locations
Elements are accessed using ______ _______.
numeric indexing
What is the syntax for accessing elements?
DataType var = ArrayName[i][j]
What is a heterogeneous 2D array?
Where each row can store different data types.
What does homogenous mean in 2D Array?
Homogeneous means that all elements in an array must be of the same data type.
What does Heterogeneity mean in 2D Array?
The presence of different types or variations within a group.
Java arrays are ________ by _______(all elements must be of the same type).
homogeneous, default
How can heterogeneity be achieved in a Java 2D array?
It can be achieved by using the Object class, which is the root of Java’s class hierarchy. By declaring a 2D array as Object[][], we can store elements of different data types.
How do you declare and initialize a heterogeneous 2D array in Java?
Use Object[][], which allows storing different data types.
How to access 2D Array Elements?
You must cast(typecasting) elements back to their original types when accessing them.
Arrays know their ______ (how many elements they can store).
length
The _______ is a public ______
so you can use _______ to access the field (arrayName.length).
length, read-only field, dot-notation
Gives the number of rows in the 2D array.
numbers.length
square matrix
(rows = columns)
Gives the number of columns in the first row.
numbers[0].length
What type of Loop to use in 2D Array?
nested loops
What does traverse mean in 2D Array?
Traverse means to go through or visit each element of a data structure one by one.
What are the Key points when using Row-Major and Colum-Major?
Row-Major Order:
*More efficient in Java.
*Outer loop iterates over rows, inner loop
iterates over columns.
Column-Major Order:
*Useful for specific use cases.
*Outer loop iterates over columns, inner loop iterates over rows.
Enhanced For Loop with 2D Arrays Uses ________ and always iterates in ______ order.
nested enhanced for loops, row-major
When using enhanced for loop. The out loop access the ____ as an _____. While the Inner loop access each _________ in the row.
row, array, element
What are the common mistakes/issues with 2D Arrays?
- Incorrect Array Indexing
- Assuming Rectangular Arrays
- Forgetting to Initialize Rows
- Confusing Row-Major and Column-Major Order
A double-dimensional (2D) array is a ________ that stores elements in a ______ format with rows and columns, allowing for the organization of data in a _______ manner.
data structure, tabular, grid-like
In Java, a 2D array is essentially an _________, where each row is a _________ array, and elements are accessed using ________: one for the row and one for the column.
array of arrays, one-dimensional, two indices
2D arrays can be ________ (all rows have the same number of columns) or _______ (rows can have varying lengths).
rectangular, jagged
They are commonly used to represent matrices, tables, or grids in applications like _________, _________, and _________.
image processing, game development, data analysis
Key operations include _______ , _______ (statically or dynamically), _______ (using nested loops in row- major or column-major order), and _______ elements using indices.
declaration, initialization, traversal, accessing