Two-Dimensional Arrays in Java
Learn two-dimensional arrays in Java to store data in rows and columns, including declaration, initialization, traversal, and practical examples.
Two-Dimensional Arrays in JavaLink to this section
A two-dimensional array in Java is an array of arrays. It is used to store data in a row and column format, similar to a table or matrix.
Two-dimensional arrays are commonly used in applications such as matrices, game boards, and tabular data processing.
Structure of Two-Dimensional ArraysLink to this section
A two-dimensional array consists of rows and columns. Each element is accessed using two indexes: one for the row and one for the column.
Example explanation: If an array has 3 rows and 4 columns, valid indexes are from [0][0] to [2][3].note
Declaring Two-Dimensional ArraysLink to this section
Declaring a two-dimensional array specifies the data type and reference variable.
Initializing Two-Dimensional ArraysLink to this section
Two-dimensional arrays can be initialized in different ways.
warning
Accessing Elements in Two-Dimensional ArraysLink to this section
Elements are accessed using both row and column indexes.
warning
Traversing Two-Dimensional ArraysLink to this section
Traversal is usually done using nested loops.
tip
Jagged Arrays in JavaLink to this section
A jagged array is a two-dimensional array where each row can have a different number of columns.
note
How many indexes are required to access an element in a two-dimensional array?
Two-Dimensional Arrays in Java
- Create a 2D array representing a 3×3 matrix and print it.
- Write a program to find the sum of all elements in a two-dimensional array.
- Find the transpose of a matrix using a two-dimensional array.
- Create a jagged array to store marks of students in different subjects.
- Explain the difference between a rectangular array and a jagged array with examples.