Open In App
Related Articles

Java Program to Print the Elements of an Array

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

An array is a data structure that stores a collection of like-typed variables in contiguous memory allocation. Once created, the size of an array in Java cannot be changed. It’s important to note that arrays in Java function differently than they do in C/C++

printing java array elements

As you see, the array of size 9 holds elements including 40, 55, 63, 17, 22, 68, 89, 97, and 89. and each element has its corresponding index value. we can use this index value i.e. array_name[Index_Value] to access the element.

Properties of Java Array

Here are some important points about Java Arrays:

  1. In Java, all arrays are dynamically allocated.
  2. Since arrays are objects in Java, user can find their length using the object property length. This is different from C/C++ where length  is calculated using function sizeof()
  3. A Java array variable can also be declared like other variables with [] after the data type.
  4. The variables in the array are ordered and each has an index beginning from 0.
  5. Java array can be also be used as a static field, a local variable, or a method parameter.
  6. The size of an array must be specified by an int value and not long or short.
  7. The direct superclass of an array type is Object.
  8. Every array type implements the interfaces Cloneable and java.io.Serializable.

How to Print an Array in Java?

There are two methods to Print an Array in Java as mentioned below:

  • Using loops
  • Using standard library arrays

1. Printing elements of an array Using loops

The algorithm used in this approach is as follows:

 Step 1: Declare and initialize an array

Step 2:  Loop through the array by incrementing the value of the iterative variable/s

Step 3: Print out each element of the array

Below is the Java example illustrating the printing elements of an array

Java

// Java Program to Print the Elements of an Array
// Using loops (considering for loop here)
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Initialize array of random numbers and size
        // Suppose array named 'arr' contains 9 elements
        int[] arr = { -7, -5, 5, 10, 0, 3, 20, 25, 12 };
  
        System.out.print("Elements of given array are: ");
  
        // Looping through array by incrementing value of i
        //'i' is an index of array 'arr'
        for (int i = 0; i < arr.length; i++) {
  
            // Print array element present at index i
            System.out.print(arr[i] + " ");
        }
    }
}

                    

Output
Elements of given array are: -7 -5 5 10 0 3 20 25 12 

The complexity of the above method

Time Complexity: O(n) Here other no major execution is taking place except just the cell memory taken by variables that even get destroyed as the scope is over. Whenever there is iteration just by using one loop time taken is of the order of n always. If nested then the order of number of loops that are nested

Space Complexity: O(n) As whatever loop is used considering the worst case where the complete array is filled up, so it takes up simply the space taken by the array in memory.

2. Printing Elements of an Array Using Standard Library Arrays

The algorithm used in the approach

Step 1: Declare and initialize an array

Step 2: Use Arrays.toString() function inside the print statement to print array 

Below is the implementation of the above method:

Java

// Java Program to Print the Elements of an Array
// Importing specific array class
// so as to use inbuilt functions
import java.util.Arrays;
  
public class GFG {
    // Main driver method
    public static void main(String[] args)
    {
  
        // Initialize array
        // Array 'arr' contains 9 elements
        int[] arr = { -7, -5, 5, 10, 0, 3, 20, 25, 12 };
  
        System.out.print("Elements of given array are: ");
  
        // Pass the array 'arr' in Arrays.toString()
        // function to print array
        System.out.println(Arrays.toString(arr));
    }
}

                    

Output
Elements of given array are: [-7, -5, 5, 10, 0, 3, 20, 25, 12]

The complexity of the above method

Time Complexity: O(n)
Space Complexity: O(n)

How to Print Java Multidimensional Array?

Printing a Multidimensional Array is more like a one-dimensional Array.

Below is the implementation of the above method:

Java

// Java Program for printing
// Java multidimensional array
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
        int[][] array
            = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  
        System.out.println(
            "Values of Multi-Dimensional Array:");
        System.out.println(Arrays.deepToString(array));
    }
}

                    

Output
Values of Multi-Dimensional Array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]


Last Updated : 15 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads