Java program to print elements of an array

Program to print element of an array

Java program to print elements of an array

In this program, we create an array and print the elements present in the array. In Java, we can initialize arrays during declaration or we can initialize later in the program as per our requirement.

Arrays are the variable that stores multiple values under the same name. A contiguous memory will be allocated to store elements. Elements of the array can be accessed through their indexes.

 

import java.io.*;
public class Array1D
{
 public static void main(String args[]) throws IOException
 {
  int[] array=new int[10];
  inti,n;
  BufferedReaderbr=new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Enter the size of array: ");
  n=Integer.parseInt(br.readLine());
  System.out.println("Enter the element of array: ");
  for(i=0;i<n;i++)
   {
    array[i]=Integer.parseInt(br.readLine());
   }
System.out.println("Element of array: ");
for(i=0;i<n;i++)
 {
  System.out.println(+array[i]);
 }
}
}

Program Output:

Enter the size of array: 5
Enter the element of array:
3
7
5
1
2
Elements of array:
3
7
5
1
2

I hope this post helps you to understand Array and its implementation in Java programming language.

Keep coding 🙂

1 thought on “Java program to print elements of an array”

Leave a Reply

Your email address will not be published. Required fields are marked *