Java One Dimensional Array Program

This article covers a simple program on one or single dimensional array in Java. The program given below, allows user to define the size of array too, along with its elements.

To print one dimensional array in Java, you need to use only one for loop, like shown in the following program.

import java.util.Scanner;

public class fresherearth
{
   public static void main(String[] args)
   {
      int n, i;
      Scanner scan = new Scanner(System.in);
      
      System.out.print("How many elements to store in array ? ");
      n = scan.nextInt();
      
      int[] arr = new int[n];
      
      System.out.print("Enter " +n+ " elements: ");
      for(i=0; i<n; i++)
         arr[i] = scan.nextInt();
      
      System.out.println("\nThe Array is: ");
      for(i=0; i<n; i++)
         System.out.print(arr[i]+ " ");
   }
}

The snapshot given below shows the sample run of above program, with user input 6 as size and 10, 20, 40, 50, 60, 100 as six elements for the array:

java program one dimensional array

You can refer to One Dimensional Array in Java, for detail.

Same Program in Other Languages

Java Online Test


« Previous Program Next Program »