Algorithm


  1. Initialize the array:

    • Declare an array of the desired data type.
    • Assign values to the array elements.
  2. Iterate through the array:

    • Use a loop (e.g., for loop or foreach loop) to iterate through each element of the array.
  3. Print each element:

    • Inside the loop, print each element of the array.

 

Code Examples

#1 Code Example- Print an Array using For loop

Code - Java Programming

public class Array {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        for (int element: array) {
            System.out.println(element);
        }
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
1
2
3
4
5

#2 Code Example- Print an Array using standard library Arrays

Code - Java Programming

import java.util.Arrays;

public class Array {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        System.out.println(Arrays.toString(array));
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
[1, 2, 3, 4, 5]

#3 Code Example- Print a Multi-dimensional Array

Code - Java Programming

import java.util.Arrays;

public class Array {

    public static void main(String[] args) {
        int[][] array = {{1, 2}, {3, 4}, {5, 6, 7}};

        System.out.println(Arrays.deepToString(array));
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
[[1, 2], [3, 4], [5, 6, 7]]
Advertisements

Demonstration


Java Programing Example to Print an Array-DevsEnv