Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Program to Print the Smallest Element in an Array
Next article icon

Java Program to Print the Elements of an Array

Last Updated : 16 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
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 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] + " ");         }     } } 

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(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));     } } 

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)

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++;         }     } } 

Output
Elements 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));     } } 

Output
Elements 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));     } } 

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


Next Article
Java Program to Print the Smallest Element in an Array

D

deepak710agarwal
Improve
Article Tags :
  • Java
  • Java Programs
  • Java-Array-Programs
Practice Tags :
  • Java

Similar Reads

  • Java Program to Print the Smallest Element in an Array
    Java provides a data structure, the array, which stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. Example: arr1[] = {2 , -1 , 9 , 10} output : -1 arr2[] = {0, -10, -13, 5} output : -13 We need to find and print the smallest value
    3 min read
  • Java Program to Print the kth Element in the Array
    We need to print the element at the kth position in the given array. So we start the program by taking input from the user about the size of an array and then all the elements of that array. Now by entering the position k at which you want to print the element from the array, the program will print
    2 min read
  • Java Program to Left Rotate the Elements of an Array
    In Java, left rotation of an array involves shifting its elements to the left by a given number of positions, with the first elements moving around to the end. There are different ways to left rotate the elements of an array in Java. Example: We can use a temporary array to rotate the array left by
    5 min read
  • Java Program to Print the Elements of an Array Present on Even Position
    The task is to print all the elements that are present in even position. Consider an example, we have an array of length 6, and we need to display all the elements that are present in 2,4 and 6 positions i.e; at indices 1, 3, 5. Example: Input: [1,2,3,4,5,6] Output: 2 4 6 Input: [1,2] Output: 2 Appr
    2 min read
  • Java Program to Print the Elements of an Array Present on Odd Position
    An array stores the collection of data of the same type. It is a fixed-size sequential collection of elements of the same type. In an array, if there are "N" elements, the array iteration starts from 0 and ends with "N-1". The odd positions in the array are those with even indexing and vice versa. E
    3 min read
  • Java Program to Find Sum of Array Elements
    Given an array of integers. Write a Java Program to find the sum of the elements of the array. Examples: Input : arr[] = {1, 2, 3} Output : 6 1 + 2 + 3 = 6 Input : arr[] = {15, 12, 13, 10} Output : 50 15 + 12 + 13 + 10 = 50 An array is a data structure that contains a group of elements. Typically th
    3 min read
  • Java Program to Sort the Elements of an Array in Descending Order
    Here, we will sort the array in descending order to arrange elements from largest to smallest. The simple solution is to use Collections.reverseOrder() method. Another way is sorting in ascending order and reversing. 1. Using Collections.reverseOrder()In this example, we will use Collections.reverse
    2 min read
  • Java Program to Sort the Elements of an Array in Ascending Order
    Here, we will sort the array in ascending order to arrange elements from smallest to largest, i.e., ascending order. So the easy solution is that we can use the Array.sort method. We can also sort the array using Bubble sort. 1. Using Arrays.sort() MethodIn this example, we will use the Arrays.sort(
    2 min read
  • Java Program to Find Largest Element in an Array
    Finding the largest element in an array is a common programming task. There are multiple approaches to solve it. In this article, we will explore four practical approaches one by one to solve this in Java. Example Input/Output: Input: arr = { 1, 2, 3, 4, 5}Output: 5 Input: arr = { 10, 3, 5, 7, 2, 12
    4 min read
  • Java Program to Increment All Element of an Array by One
    Given the array, the task is to increment each element of the array by 1. Complete traversal is required for incrementing all the elements of an array. An optimized way to complete a given task is having time complexity as O(N). Examples: Input : arr1[] = {50, 25, 32, 12, 6, 10, 100, 150} Output: ar
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences