Print Elements of an Array in Java



Array is the common way to store similar types of items together in Java. The stored items or values are referred as elements of the array. In this article, we are going to learn how to create an array and print its elements.

Printing elements of an array in Java

Use the following approaches to print elements of an array ?

  • By using for loop
  • By using while loop
  • By using Arrays.toString() method

For Loop to Print array elements

For loop is used when we know how many times a task needs to be repeated. In this case, length of the array is known, therefore, we use the for loop and print the array elements one by one.

Example

In the below example, we are using for loop to print the elements of the given arrays.

public class newarr_class {
   public static void main(String[] args) {
      // creating simple array
      int [] array1 = {11, 22, 32, 42, -52};
      // creating 2D array
      int [][] array2 = {{11, 222}, {23, 42}, {-25, 26}, {-27, 28}};
      //printing individual elements of 1D array
      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]);
      } 
      //printing all elements of 2D array
      System.out.println("\nThe 2D Int array is:\n ");
      for (int n = 0; n < array2.length; n++) {
         for (int m = 0; m < array2[n].length; m++) {
            System.out.print(array2[n][m] + " ");
         }
         System.out.println();
      }
   }
}

When you execute the code, it will print the following result ?

The elements in the 1D int array are:
11
22
32
42
-52
The 2D Int array is:
11 222 
23 42 
-25 26 
-27 28

While Loop to Print array elements

A while loop repeatedly executes a code block as long as the given condition is true. Here, we will run the loop till the array length and print its element.

Example

The following example shows how to use while loop to print array elements.

public class newarr_class {
   public static void main(String[] args) {
      // Creating a simple 1D array
      int[] array1 = {11, 22, 32, 42, -52};

      // Printing individual elements of the 1D array using a while loop
      System.out.print("\nThe elements in the 1D int array are:\n");
      int n = 0;
      while (n < array1.length) {
         System.out.println(array1[n]);
         n++;
      }
   }
}

The above code will generate the following result ?

The elements in the 1D int array are:
11
22
32
42
-52

Using Arrays.toString() to Print array elements

The Arrays.toString() method returns a string representation of the array elements.

Example

In this example, we will use Arrays.toString() method to print array elements.

import java.util.Arrays;
public class newarr_class {
   public static void main(String[] args) {
      String[] strarray = new String[]{"One", "Two", "Three", "Four", "Five"};

      // use of Arrays.toString()
      System.out.println(Arrays.toString(strarray));
   }
}

On running this code, it will show the below output ?

[One, Two, Three, Four, Five]
Updated on: 2024-07-30T17:00:39+05:30

882 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements