## The course is part of this learning path

In this course, we'll learn about Arrays in Java.

### Learning Objectives

- What is an Array?
- Declaration and Initialization
- Sorting Arrays
- Searching Arrays
- Multi-Dimensional Arrays

### Intended Audience

- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

### Prerequisites

- No prior knowledge is required about the Java programming language.
- Basic computer knowledge

Hi there. In this lesson, we'll talk about Multi-Dimensional Arrays. The array that we have learned and made examples of so far is an example of one-dimensional arrays, but in Java there are also multi-dimensional arrays. We'll talk about these in this lesson. Multi-dimensional arrays are arrays that contain more than one array. You can also think of it as a nested array. These arrays are saved in tables. A one-dimensional array consists of a single row while a two-dimensional array is represented by a table consisting of rows and columns. We use multiple square brackets when creating these arrays. The dimension of the arrays i.e. the number of rows and columns are also indicated in the square brackets. In this lesson, we'll make examples of 2D and 3D arrays. Let's take a closer look at two-dimensional arrays first. We use two square brackets when describing two-dimensional arrays.

Since a two-dimensional array will now be resented by tables, this time, the number of rows and columns enters the circuit. The first square bracket represents the number of rows. The second square bracket also represents the number of columns. Actually, each one-dimensional array is represented by the rows. Each element of a one-dimensional array is represented by the columns. If we look at the syntax of the two-dimensional array, this array consists of three rows, that is three one-dimensional arrays. Also, each array consists of four elements, that is, there are four columns in this 2D array. Each array is indicated in curly braces. A comma is also placed between each array. All these arrays are within the scope of the two-dimensional array. Let's take a look at the table view of this 2D array. Notice that each row represents a one-dimensional array. Also, each element in the array corresponds to the columns respectively.

For example, the first element of the first array is at the point [0][0], representing the first row and the first column. The second element of the first array is at the point [0][1], which represents the first row and the second column. For example, the second element of the third array is at the point [2][1] which represents the third row and the second column. Now let's look at three-dimensional arrays. Three-dimensional arrays are made up of two-dimensional arrays. Three square brackets are used when defining three-dimensional arrays. The first square bracket represents each number of tables, that is, the total number of two-dimensional arrays in that array. The second square bracket again represents the number of rows in each 2D array or in other words, the number of arrays in each 2D array. The third square bracket also specifies the number of elements in each one-dimensional array or the number of columns. Now let's look at how to create a 3D array. As you can see, three two-dimensional arrays came together to form a three-dimensional array. Also, there are one-dimensional arrays inside each two-dimensional array.

There is one curly brace covering all these arrays, which is the scope of the 3D array. Also, each one-dimensional and two-dimensional array is separated from the other by a comma. Now, let's look at the representation of this series in tabular form. As you can see, each two-dimensional array is actually represented by a table, represents a one-dimensional arrays in rows in each table. In short, one-dimensional arrays come together to form two-dimensional arrays while two-dimensional arrays come together to form a three-dimensional array. Here, you can pause the video and examine this table in detail but now it's time to move on to Eclipse and get some practice. First, I will create a new class. I will right click on the array package and select the new class options. The class can be MultiDArrays and I will check the checkbox for the main method and click the 'Finish' button. Okay, first, let's make an example with a two-dimensional array. I'll create a two-dimensional array with an int type. int[][] nums = new int[][]; Now we must specify the number of rows and columns. For example, this array can be 2 rows and 3 columns and let's comment 2 rows and 3 columns. That means this array will consist of two one-dimensional arrays and each array will consist of three elements.

Also, the total elements of this array are six because the multiply of rows and columns will give the total element in this array. Now let's add the elements of this array. nums[0][0], first will be 0, second will also be 0. That means the first element of the first array or first element of the first row. Let the value of this element be 1. Now I'll copy this line and paste it below five times. The index of the second will be 0 and 1 which means the second element in the first row and its value can be 3. The index of the third will be 0 and 2, which means the third element in the first row and its value can be 5. So, the first row is ready. If we try to add another element to the first row, we'll get an error this time but the error we get is not a compilation error but an index out of bounds error. So, when we run the application, the application will crash. Do not forget this. Just like when we try to print the missing element in one-dimensional arrays. Okay, let's continue. We can specify the elements of the second array or second row. The index of the first element of the second array will be 1 and 0, which means the first element in the second row and its value can be 7. The index of the second element of the second array will be 1 and 1 which means the second element in the second row and its value can be 9. The index of the third element of the second array will be 1 and 2, which means the third element in the second row and its value can be 11. Now, let's print these on the console. System.out.println(Arrays.toString(nums));

Let's run the code. As you can see, we printed the ID for each one-dimensional array and the two-dimensional array. If you notice, this expression begins with double square brackets which means this is a two-dimensional array and this array consists of two one-dimensional arrays. Now let's print each element of these arrays. I'll put a square bracket here and type 0. This will print the elements in the first row or elements of the first array, and I'll copy and paste this line. And this will be 1 for the second row or second array. Now let's run and see. As you can see, this time we printed the elements of the two-dimensional array. Okay, now let's create another two-dimensional array. This type of array will be int again, and the name of this array can be nums2 and I'll put one of the square brackets after the name of the array. This is also valid and this time after the equal sign, I'll specify the elements of this array inside the curly braces. First, I'll open curly braces. This will represent the scope of the two-dimensional array. And in these curly braces, I'll open two more curly braces and I'll put a comma between these curly braces.

These curly braces will represent each one-dimensional array in the two-dimensional array. Also, you can create many more arrays here but I'll just show you how to create them. Let these arrays consist of three elements again. The elements of the first array can be {2, 4, 6}, the elements of the second array can be {8, 10, 12}. Okay, the array is ready. Note that the inner arrays are separated by commas and the outermost curly braces have semicolons at the end. These two points are often forgotten. Now, let's print these arrays on the console again. I'll copy these print methods and paste them here. The name of the array will be nums2. Okay, let's run and see. As you can see, the elements of the two-dimensional arrays are on the console. Okay, this time let's create a three-dimensional array, int[][][] nums3. Also, you can write the square brackets after the name of the array. There is no problem. Now, let this array consists of two two-dimensional arrays and each two-dimensional array also consists of two one-dimensional arrays. So, after the equal sign, I open the curly braces. This will be the scope of the three-dimensional array. Also, I'll put a semicolon at the end of the curly braces. Okay, also I'll open two more curly braces inside this. These will represent each two-dimensional array.

Also, I'll put a comma between these curly braces, don't forget this. Now inside these curly braces, I'll create the one-dimensional array. Let's create the first array in the two-dimensional array. The elements of this array can be 10, 30 and 50. Also, I'll put a comma, and now let's create the second one-dimensional array in the first two-dimensional array. The elements of this array can be 70, 90 and 110. Okay, the first two-dimensional array is ready. Now, I'll create the second two-dimensional array so I will copy these and paste them here, and I'll change the elements. The elements of the first one-dimensional array a the second two-dimensional array can be 20, 40 and 60. And the elements of the second one-dimensional array and the second two-dimensional array can be 80, 100 and 120. Okay, now let's print these on the console. System.out.println(Arrays.toString(nums3)); let's run and see.

As you can see, we printed the ID for each one-dimensional array in the first two-dimensional array. If you notice, this expression begins with triple square brackets, which means this is a three-dimensional array. Now, let's print each element of these arrays. I'll put a square bracket here and type 0 and I will put one more square bracket and type 0. This will print the elements in the first one-dimensional array in the first two-dimensional array, and I'll copy this line and paste it below three times. This will be 0 and 1. This will be 1 and 0, and this will be 1 and 1. Let's run and see. As you can see, this time we printed all elements in the three-dimensional array. Yes, so we've come to the end of multi-dimensional arrays. I hope it was useful for you. Let's take a short break here. See you in the next lesson.

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.