Java Program to Print the Elements of an Array


In this article, Array elements are selected for printing the elements by using their index value. The array is the common way to store similar types of items together in java. The individual values as well as all elements of the array can be printed easily. For printing, for all elements of an array a "for loop" is commonly used which selects the index from 0 up to the length of the array.

Following are the few examples of integer and string type array

Int type array example

int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210};
int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}};
int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};

String type array examples

String[] strarray = new String[]{"One", "Two", "Three"};
String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}};
String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};

Algorithm

  • Step 1 − Declare the type and define the array.

  • Step 2 − Specify the elements based on the type of array. The elements can also be entered by the user.

  • Step 3 − Start with the index 0 elements. Print it.

  • Step 4 − Increase the index by 1 and print the next element.

  • Step 5 − Go to step 4 and keep doing the same till the last element of the array is printed.

  • Step 6 − For 2D arrays, two separate loops are used to control the row index and column index separately.

  • Step 7 − For N Dimensional arrays N loops are used to control the N indexes separately.

Multiple Approaches

We have provided the solution example separately using different types.

  • By using Int-type Arrays

  • By using String type Arrays

Let’s see the program along with its output one by one.

Approach / Example Type 1: Using Integer Types Arrays

For One Dimensional Array

for (int n=0; n<array1.length; n++){
   System.out.println(array1[n]);
} ;

For Two-Dimensional Array

for (int n = 0; n < 2; n++) {
   for (int m=0; m< 2; m++) {
      System.out.print(array2[n][m] + " ");
   }
   System.out.println();
}

For Three-Dimensional Array

System.out.println("\nThe 3D Int array is:\n ");
for (int n = 0; n < 3; n++)
for (int m=0; m< 2; m++)
for (int t = 0; t < 2; t++)
   System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]);
};

Example

public class newarr_multidim {
   public static void main(String[] args) {
      int [] array1 = {11,22,32,42,-52,62,-72,82,-92,210};
      int [][] array2 = {{11,222},{23,42},{-25,26},{-27,28},{-29,120}};
      int [][][] array3 = {{{1111, -22222},{5533, 433}},{{44533, -635533},{-777733, 84433}},{{90033, 84433},{-999933, 433}}};

      //printing individual elements by index value
      System.out.println(array1[1]+ "\n\n" +array2[0][1] + "\n\n" + array3[1][0][1]);

      //printing all elements
      System.out.println("\nThe elements in the 1D int array are:\n");
      for (int n=0; n<array1.length; n++){
         System.out.println(array1[n]);
      } ;
      System.out.println("\nThe 2D Int array is:\n ");
      for (int n = 0; n < 2; n++) {
         for (int m=0; m< 2; m++) {
            System.out.print(array2[n][m] + " ");
         }
         System.out.println();
      }
      System.out.println("\nThe 3D Int array is:\n ");
      for (int n = 0; n < 3; n++)
      for (int m=0; m< 2; m++)
      for (int t = 0; t < 2; t++)
      System.out.println("array3[" + n + "][" + m + "][" + t + "] = " + array3[n][m][t]);
   };
}

Output

22
222
-635533
The elements in the 1D int array are:
11
22
32
42
-52
62
-72
82
-92
210
The 2D Int array is:
11 222
23 42
The 3D Int array is:
array3[0][0][0] = 1111
array3[0][0][1] = -22222
array3[0][1][0] = 5533
array3[0][1][1] = 433
array3[1][0][0] = 44533
array3[1][0][1] = -635533
array3[1][1][0] = -777733
array3[1][1][1] = 84433
array3[2][0][0] = 90033
array3[2][0][1] = 84433
array3[2][1][0] = -999933
array3[2][1][1] = 433

Approach / Example Type 2: Using String Type Arrays

For One Dimensional Array

for (int n=0; n<strarray.length; n++){
   System.out.println(strarray[n]);
} ;

For Two Dimensional Array

for (int n = 0; n < 2; n++) {
   for (int m=0; m< 2; m++) {
      System.out.print(strarray2[n][m] + " ");
   }
   System.out.println();
}

For Three Dimensional Array

System.out.println("\nThe 3D String array is:\n ");
for (int n = 0; n < 3; n++)
for (int m=0; m< 2; m++)
for (int t = 0; t < 2; t++)
System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]);
};

Example

public class newarr_multidim2 {
   public static void main(String[] args) {
      String[] strarray = new String[]{"One", "Two", "Three"};
      String[][] strarray2 = new String[][]{{"One1", "Two2"}, {"Three3", "Four"}};
      String[][][] strarray3={{{"One33", "two33"},{"three33", "433"}},{{"44533", "635533"},{"seven33", "84433"}},{{"seven33", "84433"},{"three33", "433"}}};

      //printing some elements by index value
      System.out.println(strarray[1]+ "\n\n" +strarray2[0][1] + "\n\n" + strarray3[1][0][1]);

      //printing all elements
      System.out.println("\nThe 1D String array is:\n ");
      for (int n=0; n < strarray.length; n++){
         System.out.println(strarray[n]);
      } ;
      System.out.println("\nThe 2D String array is:\n ");
      for (int n = 0; n < 2; n++) {
         for (int m=0; m< 2; m++) {
            System.out.print(strarray2[n][m] + " ");
         }
         System.out.println();
      }
      System.out.println("\nThe 3D String array is:\n ");
      for (int n = 0; n < 3; n++)
      for (int m=0; m< 2; m++)
      for (int t = 0; t < 2; t++)
      System.out.println("strarray3[" + n + "][" + m + "][" + t + "] = " + strarray3[n][m][t]);
   };
}

Output

Two
Two2
635533
The 1D String array is:
One
Two
Three
The 2D String array is:
One1 Two2
Three3 Four
The 3D String array is:
strarray3[0][0][0] = One33
strarray3[0][0][1] = two33
strarray3[0][1][0] = three33
strarray3[0][1][1] = 433
strarray3[1][0][0] = 44533
strarray3[1][0][1] = 635533
strarray3[1][1][0] = seven33
strarray3[1][1][1] = 84433
strarray3[2][0][0] = seven33
strarray3[2][0][1] = 84433
strarray3[2][1][0] = three33
strarray3[2][1][1] = 433

Conclusion

In the above article, the examples of Int and String types are used to print the array elements using Java language. The instances include one-dimensional array elements printing, 2D array elements printing, and 3D array elements printing. These element printing approaches are extendable to N-dimensional arrays.

Updated on: 23-Mar-2023

626 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements