Java Program to Create an Array and Fill it with Random Integer Values

In the previous article, we have seen Java Program to Convert JSON Array to String Array

In this article we are going to see how to fill an array with random integers using java programming language.

Java Program to Create an Array and Fill it with Random Integer Values

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to fill an array with random integers.

Note: To get the Random int values we utilize the java.util.Math provided under java library. Math.random() returns random double value between 0 and 1.

Method-1: Java Program to Create an Array and Fill it with Random Integer Values By Static Initialization of Array Elements

Approach:

  • Initialize an array.
  • Loop over the array.
  • At each index call Math.random() and insert the value returned by it.

Program:

public class Main
{
    public static void main(String[] args)
    {
        Double[] arr = new Double[10];
        System.out.print("Random Array: [");
        for (int i = 0; i < arr.length; i++) 
        {
            //creating the random array
            arr[i] = Math.random();
            System.out.print(arr[i] + " ");
        }
        System.out.print("]");

    }
}
Output:

Random Array: [0.6341582513475396 0.5865128679607265 0.7506819101712918 0.6664440406163092 0.05870216052896149 0.34985829957035364 0.12036321353203538 0.16406839003765872 0.8337720042760279 0.42280958788946654 ]

Method-2: Java Program to Create an Array and Fill it with Random Integer Values By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask the user for number of elements in the array.
  • Initialize an array.
  • Loop over the array.
  • At each index call Math.random() and insert the value returned by it.

Program:

import java.util.Scanner;

public class Main 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the array: ");
        Double[] arr = new Double[sc.nextInt()];
        System.out.print("Random Array: [");
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i] = Math.random();
            System.out.print(arr[i] + " ");
        }
        System.out.println("]");
    }
}

Output:

Enter the size of the array: 5
Random Array: [0.07348015542650743 0.040921197914756124 0.753663183321657 0.9853337882332339 0.36985835596713135 ]

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: