Java Program to Print the Elements of an Array
Last Updated :
16 Jul, 2024
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++

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:
- In Java, all arrays are dynamically allocated.
- 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()
- A Java array variable can also be declared like other variables with [] after the data type.
- The variables in the array are ordered and each has an index beginning from 0.
- Java array can be also be used as a static field, a local variable, or a method parameter.
- The size of an array must be specified by an int value and not long or short.
- The direct superclass of an array type is Object.
- 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 for loop
- Using standard library arrays
- Using while loop
- Using forEach() method
1. Printing elements of an array Using for loop
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 for loop
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] + " ");
}
}
}
OutputElements 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(1) because the algorithm uses only a constant amount of extra space for variables (arr
, i
, and System.out
), regardless of the size of the input array.
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));
}
}
OutputElements 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)
3. Printing elements of an array Using while loop
The algorithm used in the approach
Step 1: Declare and initialize an array.
Step 2: Use the while loop to iterate over the array elements.
Step 3: Print out each element of the array.
Below is the implementation of the above discussed approach:
Java
// Java Program to Print the Elements of an Array
// Using while loop
public class GFG {
// main method
public static void main(String[] args) {
// Initialize array of random numbers and size
int[] arr = { -17, 15, 5, 10, 0, 3, 18, 25, 12 };
System.out.print("Elements of the given array are: ");
// while loop to iterate through array
int i = 0;
while (i < arr.length) {
// Print array elements
System.out.print(arr[i] + " ");
i++;
}
}
}
OutputElements of the given array are: -17 15 5 10 0 3 18 25 12
The complexity of the above code:
Time Complexity: O(n )
Space Complexity: O(1) or Constant
4. Printing elements of an array Using forEach() method
The algorithm used in the approach:
Step 1: Declare and initialize an array.
Step 2: Use Arrays.stream() to convert the array to a stream.
Step 3: Then print array elements using forEach() method
Below is the example of printing elements of an array using forEach() method:
Java
// Java Program to Print the Elements of an Array
// Using forEach() method
import java.util.Arrays;
public class GFG {
// main method
public static void main(String[] args) {
// Initialize array of random numbers and size
int[] arr = { -17, 15, 5, 10, 0, 3, 18, 25, 12 };
System.out.println("Elements of the given array are: ");
// Using Arrays.stream() to convert the array to a stream
Arrays.stream(arr)
// printing the elements
.forEach(elem -> System.out.println(elem));
}
}
OutputElements of the given array are:
-17
15
5
10
0
3
18
25
12
The complexity of the above approach:
Time Complexity: O(n)
Space Complexity: O(1) or Constant
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));
}
}
OutputValues of Multi-Dimensional Array:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read