The two-dimensional array is a collection of items that share a common name and they are organized as a matrix in the form of rows and columns. The two-dimensional array is an array of arrays, so we create an array of one-dimensional array objects.
We can create a 2D array in two ways:
- Using a nested for loop
- Array manually with the array literal notation
The following program shows how to create a 2D array :
Example 1: In this example, we will construct a two-dimensional array using integer values.
Creating 2D array [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ]
Example 2: In this example, we will create a two-dimensional array using string values.
[ [ 'G', 'e', 'e' ], [ 'k', 's', 'f' ], [ 'o', 'r', 'G' ] ]
Example 3: In this example, we will create a 2D array manually. This is the 2D array that stores the information of the student.
[ [ 'Bar', 20, 60, 'A' ], [ 'Foo', 10, 52, 'B' ], [ 'Joey', 5, 24, 'F' ], [ 'John', 28, 43, 'A' ], [ 'Liza', 16, 51, 'B' ] ]
Please Login to comment...