In this post, we will create an array and populate the array with some random values.
Declare an array
We can declare an array of integers using the syntax:
int[] array;
Create an array with random values
We can create an array of ten integers using the syntax.
array = new int[10];
Java program
import java.util.Random; public class RandomArrayDemo { public static void main(String[] args) { int[] array; // declare an array array = new int[10]; // create an array of ten integers Random rand =new Random(); for (int i = 0; i < array.length; i++) { array[i] = rand.nextInt(100); // generate random number System.out.print(array[i] + " "); } } }
Sample Output
40 26 32 73 5 8 15 16 86 25
Note that : As expected for each run of the program the values in the array would change.
Screenshot
—
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/
For more information on Java, visit the official website :
https://www.oracle.com/in/java/