In the last article, we explored what an Array data structure is, how it manages its elements, and, most importantly, the different applications of an Array.
In this article, we will discuss the classification of the Array data structure. There are two different types of Arrays:
1. One-Dimensional Array | 1-D Array
2. Multi-Dimensional Array
a. Two-Dimensional Array | 2-D Array
b. Three-Dimensional Array | 3-D Array
• A One-Dimensional Array is also called a 1-D Array or Single Dimensional Array.
• A One-Dimensional Array is a collection of elements of the same type. It means that in a One-Dimensional Array, we create an array and store values of a compatible type.
• In Java, we can create a One-Dimensional Array in two different ways:
class JTC{
public static void main(String arg[]){
// Creating a Static Array
int[] ar1 = {101,202,303,404,505};
// Traversing the Array from 0th Index to Last Index
for(int i = 0; i <= ar1.length-1; i++){
System.out.println("Index :- "+i+" | Value :- "+ar1[i]);
}
}
}
Index :- 0 | Value :- 101 Index :- 1 | Value :- 202 Index :- 2 | Value :- 303 Index :- 3 | Value :- 404 Index :- 4 | Value :- 505
In the example above, we have a class JTC that contains a main method. Inside the main method, we have a one-dimensional static array created as follows: int[] ar1 = {101, 202, 303, 404, 505};. In this statement, ar1 is a reference variable, and its data type is int[], representing a one-dimensional array of integers. The values {101, 202, 303, 404, 505} constitute the array (which is an object in Java), and its reference is stored in ar1.
In the next part of the example, we use a for loop to traverse the ar1 array in the forward direction, meaning from index position 0 to the last index position.
for ( int i = 0; i <= ar1.length-1; i++){
System.out.println ("Index: - "+i+" | Value: - "+ar1[i]);
}
In this for loop code, int i = 0 means that when the control enters the loop, a local variable i is created and initialized to 0.
Next, the control moves to the condition part of the loop, where we have i <= ar1.length - 1. The length property is a built-in variable in Java that represents the number of elements in the array. Here, we're checking a condition to ensure that the loop iterates from the 0th index position to the last index position.
If the condition is true, the control enters the loop body, where the code System.out.println("Index: - " + i + " | Value: - " + ar1[i]) is executed. This line is dedicated to printing the current index and its corresponding value on the console.
Finally, the control moves to the update section of the loop, where i++ increments the value of i by 1. This process continues until the condition i <= ar1.length - 1 becomes false, at which point the loop terminates.
class JTC{
public static void main(String arg[]){
// Creating a Dynamic Array and length is 5.
int[] ar1 = new int[5];
// Traversing the Array from 0th Index to Last Index
for(int i = 0; i <= ar1.length-1; i++){
System.out.println("Index :- "+i+" | Value :- "+ar1[i]);
}
// Dynamically injecting the data into the given array
ar1[0] = 101;
ar1[1] = 102;
ar1[2] = 103;
ar1[3] = 104;
ar1[4] = 105;
// Traversing the Array from 0th Index to Last Index
for(int i = 0; i <= ar1.length-1; i++){
System.out.println("Index :- "+i+" | Value :- "+ar1[i]);
}
}
}
Index :- 1 | Value :- 0 Index :- 2 | Value :- 0 Index :- 3 | Value :- 0 Index :- 4 | Value :- 0 Index :- 0 | Value :- 101 Index :- 1 | Value :- 102 Index :- 2 | Value :- 103 Index :- 3 | Value :- 104 Index :- 4 | Value :- 105
In the above example, we are creating a Dynamic 1-D Array with the statement int[] ar1 = new int[5];. This initializes an array ar1 with a length of 5. After creating the array, we access and traverse the declared array in the forward direction using a for loop.
In the next section, we inject data into the array explicitly, such as ar1[0] = 101, ar1[1] = 102, and so on. It's important to note that we can populate the array with data from any external source, such as a database, Excel sheet, or keyboard input.
Finally, we traverse the array again from the 0th index position to the last index position using a for loop to process or display the data.
Note: In both examples, we see the creation of a one-dimensional array using both static and dynamic methods. In both cases, we are directly storing elements into the array, which is why we refer to a 1-D array as an array of elements.
• Multi-Dimensional Array is called an Array of arrays; it means in multi-dimensional array we get an array which will hold another array reference.
• Multi-Dimensional Array is divided into 2 parts:
a. Two Dimensional Array or 2-D Array
b. Three Dimensional Array or 3-D Array
• In Java we can create 2-D Array using 2 different ways:
• Static 2-D Array
• Dynamic 2-D Array
class JTC{
public static void main(String arg[]){
// Creating Static 2-D Array
int[][] ar1 = {{101,202,303},{-11,23,56,89},{-90,11}};
// Traversing 2-D Array using for-loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
}
}
101 202 303 -11 23 56 89 -90 11
In the example above, we are declaring a static 2-D array with the statement int[ ][ ] ar1 = {{101, 202, 303}, {-11, 23, 56, 89}, {-90, 11}};. Here, ar1 is a reference variable, and int[ ][ ] is the data type (a 2-D array of integers) that stores references to other arrays.
At the 0th index of the first array, we have the reference to {101, 202, 303}, which means ar1[0] = {101, 202, 303}. At the 1st index of the first array, we have the reference to {-11, 23, 56, 89}, which means ar1[1] = {-11, 23, 56, 89}. Finally, at the 2nd index of the first array, we have the reference to {-90, 11}, which means ar1[2] = {-90, 11}.
In the next section of the example, we use nested for loops to traverse the elements of the inner arrays one by one.
class JTC{
public static void main(String arg[]){
// Creating Dynamic 2-D Array
int[][] ar1 = new int[3][4];
// Traversing 2-D Array using for-loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
// Storing Data Dynamically using nested for loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
ar1[i][j] = 100+i+j;
}
}
// Traversing 2-D Array using for-loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
System.out.print(ar1[i][j]+" ");
}
System.out.println();
}
}
}
0 0 0 0 0 0 0 0 0 0 0 0 100 101 102 103 101 102 103 104 102 103 104 105
In the above program, we created a dynamic 2-D array with the statement `int[ ][ ] ar1 = new int[3][4];`. Here, the first dimension of the array has a length of 3, creating 3 nested arrays, each with a length of 4.
After that, we access the elements of the nested arrays using a nested `for` loop. Next, we dynamically inject values into the nested arrays using another nested `for` loop. Finally, we traverse the array again using a nested `for` loop to process or display the data.
• In Java we can create 3-D Array using 2 different ways:
• Static 3-D Array
• Dynamic 3-D Array
class JTC{
public static void main(String arg[]){
// Creating Static 3-D Array
int[][][] ar1 = {{{101,202,303},{404,505},{-11,45,23,78}},{{11},{78,45}}};
// Traversing static 3-d array using 3 nested for-loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
for(int k = 0; k <= ar1[i][j].length-1; k++){
System.out.print(ar1[i][j][k]+" ");
}
System.out.println();
}
}
}
}
101 202 303 404 505 -11 45 23 78 11 78 45
In this program, we create a static 3-D array with the declaration int[ ][ ][ ] ar1 = {{{101, 202, 303}, {404, 505}, {-11, 45, 23, 78}}, {{11}, {78, 45}}};. Here, ar1 is a reference variable, and int[ ][ ][ ] is the data type of the ar1 variable, representing a 3-D array of integers.
ar1 contains references to two 2-D arrays. The first 2-D array has a length of 3, and the second 2-D array has a length of 2.
Within these 2-D arrays, there are a total of 5 one-dimensional arrays of integers, whose references are stored in the 2-D arrays.
In the next section of the program, we print the elements of these nested one-dimensional arrays using three nested for loops.
Note: For better understanding, refer to the diagram that illustrates the above example.
class JTC{
public static void main(String arg[]){
// Creating Dynamic 3-D Array
int[][][] ar1 = new int[2][3][3];
// Traversing Dynamic 3-D Array using 3 nested for loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
for(int k = 0; k <= ar1[i][j].length-1; k++){
System.out.print(ar1[i][j][k]+" ");
}
System.out.println();
}
}
// Dynamicallt injecting the data into the dynamic 3-d array using 3 nested for-loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
for(int k = 0; k <= ar1[i][j].length-1; k++){
ar1[i][j][k] = 100+i+j+k;
}
}
}
// Traversing Dynamic 3-D Array using 3 nested for loop
for(int i = 0; i <= ar1.length-1; i++){
for(int j = 0; j <= ar1[i].length-1; j++){
for(int k = 0; k <= ar1[i][j].length-1; k++){
System.out.print(ar1[i][j][k]+" ");
}
System.out.println();
}
}
}
}
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 101 102 101 102 103 102 103 104 101 102 103 102 103 104 103 104 105
In the above example, we create a dynamic 3-D array with the statement int[][][] ar1 = new int[2][3][3];. This initializes a 3-D array with 2 blocks, each containing 3 rows and 3 columns.
We then inject values dynamically into this 3-D array. Finally, we use three nested for loops to print the values to the console.