Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Algorithm
3.1.
Implementation
3.2.
Output
4.
Frequently Asked Questions
5.
Conclusion
Last Updated: Mar 27, 2024

Program to Print the Elements of an Array

Author Aditya Anand
0 upvote

Introduction

In Java, an array is a collection of variables with similar types referred to by a common name. Let’s start by recalling some of the essential points about Java arrays.

  1. Arrays are objects in Java and are allocated dynamically. We can use the object property length to determine its length.
  2. A Java array variable can also be declared in the same way that other variables are, by appending [] after the data type.
  3. The array's variables are ordered, and each has an index starting at 0.
  4. Java arrays can also be used as static fields, local variables, and method parameters.
  5. An array's size must be specified as an int or short value, not a long value.


In this article, we’ll learn how to print elements of an array one by one. So, let’s get started!

Problem Statement

Write a Java program to print the elements of an array one by one.

Example: Let the array given by:

Arr = 1,2,3,4,5

Output: 1 2 3 4 5

In this problem, we must create an array and print the array's elements. In the example above, 1, 2, 3, 4 and 5 represent the elements of the array. These elements are accessible via their corresponding indexes, which are 0, 1, 2, 3, and 4.

Must Read Array of Objects in Java

Algorithm

Arrays are a type of variable that stores multiple values with the same name. To store elements, contiguous memory space will be allocated. The array's elements can be accessed using their indexes.


The steps involved in printing array elements are

  1. Declare and initialise an array.
  2. By incrementing the value of “i”, you can loop through the array.
  3. Finally, print out each array element.

Implementation

The Java program to print the elements of an array are as follows:

public class PrintArrayElements {  
   public static void main(String[] args) {      
         
       //Declare and initialise the array 
       int [] arr1 = new int [] {11, 12, 13, 14, 15};   
         
       System.out.println("Elements of the array are as follows:");  
       //Loop through the array by incrementing value of i   
       for (int i = 0; i < arr1.length; i++) 
       {   
           int j=i+1;
           System.out.print("The element number "+ j + " is " + arr1[i] + "\n");   
       }    
   }  
}
You can also try this code with Online Java Compiler
Run Code

Output

Practice it on online java compiler for better understanding.

Also check out Addition of Two Numbers in Java here.

Frequently Asked Questions

  1. In Java, what memory arrays are created?
    JVM creates arrays in dynamic memory. In Java, there is no such thing as static memory; everything (variable, array, object, etc.) is created on dynamic memory only.
     
  2. What are the different types of arrays?
    Arrays are generally classified into two types as described below: 
    a. Array of One Dimension
    b. Array in Multiple Dimensions (2D and 3D arrays)
     
  3. Is it possible to declare the size of an array to be negative?
    No, array size cannot be declared as negative. Even so, there will be no compile-time error if we declare the negative size. However, at runtime, we get the NegativeArraySizeException.

Conclusion

In this article, we saw how to iterate through an array in java and print its elements. 


Java is one of the most popular object-oriented programming languages. If you are a beginner in Java, it is recommended to go through the object-oriented concepts like Inheritanceabstractionpolymorphism etc. to get familiar with the language. 

Recommended problems -

 


For technical interviews, you can refer to some of the frequently asked OOPs Interview Questions to refresh your concepts.


Keep Learning with Coding Ninjas!

Live masterclass