‫Welcome back.
‫In this video, we'll learn about multidimensional arrays in Java, a multidimensional array is an array
‫of arrays.
‫It allows you to create a table like structure where elements are arranged in rows and columns.
‫This enables you to work with data that has multiple dimensions, such as a 2D grid matrix or data organized
‫in rows and columns.
‫To create a multi-dimensional array, you define the array with multiple sets of square brackets.
‫The general syntax of creating a 2D array is as follows.
‫We start with the data type.
‫The data type is the data type of the elements in the array.
‫For example, we can replace it with int string byte long or other objects or classes and so on.
‫Then we follow where with the two double square brackets, because we're working with 2D array here,
‫we specify the array name, the array name of the array variable, equal new keyword.
‫And here we specify again the data type with the two square brackets.
‫Inside the square brackets, we specify the rows and columns.
‫The rows and columns should be the number of rows into the array and the number of columns in the 2D
‫array respectively.
‫Columns.
‫I'll create a 2D array with three rows and three columns.
‫So to create this array, I'll follow this syntax.
‫I'll start with the data type, which is integer for INT because I need this matrix or this array to
‫the array to store integers.
‫Then I specify the array name, the array name, I'll name it as my matrix.
‫Okay, equal to new int.
‫And here I'll specify the number of rows and number of columns.
‫Here I'll specify number of rows and here number of columns.
‫So following this syntax, I created this 2D array that holds numbers that stores numbers in form of
‫integer data type.
‫Now let's learn how to store values in the 2D array.
‫We have created this array, my matrix.
‫And let's start with my matrix.
‫And here we specify my at row zero and column zero the value of one.
‫You.
‫Did you understand it?
‫Guys, it's very simple.
‫We start with the rows and then we increase the.
‫Columns.
‫My matrix.
‫We start with zero and one.
‫Here is the second column.
‫And first row.
‫Remember, the index starts with zero.
‫To my matrix zero.
‫Do.
‫Equal to three, then we need to move to the next row because we finish filling the columns.
‫We have three columns, number one, two and three.
‫To move to the to the next or the second row.
‫We'll start with my matrix and here we'll increase it by one.
‫Then again, I need to access the second row first column.
‫So I put the index zero and here I'll fill it with four.
‫And so on until we fill the nine.
‫Cells.
‫And here we go.
‫We created a matrix three rows and three columns.
‫We have a result of nine cells.
‫We need to fill them and we fill them from 1 to 9.
‫In order to access the elements in the 2D array and to print them, we specify the index of the row
‫and the column.
‫I'll start with system dot out, dot print, and here I'll specify my matrix and I need to print the
‫number seven, which is placed on the second, the third row and first column.
‫So I'll specify here the third row with the first column.
‫Okay.
‫Let me run.
‫And the result is seven.
‫So in order to access elements of a 2D array, we use two sets of square brackets specifying the row
‫and column indices.
‫Another way to create and initialize a 2D array, we specify the data where the predefined values we
‫define the matrix with predefined values.
‫For example, I'll remove this and I need to start with this curly brackets and I'll open another one
‫here.
‫The first row will have one, two and three.
‫This is the first array, comma, the second array, four, five and six.
‫The third array is seven, eight and nine.
‫So remember, the multidimensional array is an array of arrays.
‫So the inner curly braces represents the rows and the outer curly braces represents the to the array
‫itself.
‫You can have multidimensional arrays with more than two dimensions, such as 3D array or even higher
‫dimensional arrays.
‫The concept is the same.
‫You add more sets of square brackets for each additional dimension.
‫So multidimensional arrays in Java provide a powerful way to organize and work with complex data structures
‫efficiently.