Array is a collection of variables of the same type stored in contiguous memory locations.
It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables. Instead of declaring individual variables, such as number0, number1, ..., number99, you can declare one array variable such as numbers and use numbers[0], numbers[1], ..., numbers[99] to access the individual variables.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. An array can hold many values under a single name, and you can access the values by referring to the index number.
Index number is the position of the element in the array.
It starts from 0 to the last being (size of array - 1)
For instance if the size of an array is 10, the indexes will be as follows
| Element Number | Index Number |
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 5 |
| 7 | 6 |
| 8 | 7 |
| 9 | 8 |
| 10 | 9 |
As you can see, the last index number is one less than the size of the array because the index number starts from 0.
You can picture an array as a bookshelf where books are stored.
The books represent the data/elements to be stored and must have the same data type, as it is inappropriate to place other items which are not books on a bookshelf.
For easy location of the books, the shelf is numbered so you know which book is at what shelf number.
The shelf number (slot) represents the index number. In this case, the shelf numbering starts from 0.

Arrays in Java can be declared in any of the following ways:
data_type[] array_name = new data_type[size];data_type[] array_name = {value1, value2, ..., valueN};Where data_type is the type of data to be stored in the array, array_name is the name of the array, and size is the number of elements that can be stored in the array.
When you declare an array using the new keyword, you must specify the type of data it will hold and the number of elements it can store.
For example, to declare an array of integers that can hold 5 elements, you can use the following syntax:
int[] numbers = new int[5];
This declaration creates an array object named numbers that can hold 5 integer values.
You can also initialize an array directly with values without explicitly specifying the size:
int[] numbers = {1, 2, 3, 4, 5};
In the above declaration, the Java compiler automatically determines the size of the array based on the number of elements provided (size 5).
Once an array is declared, you can assign values to its elements using the index number:
numbers[index] = value;
Where index is the position of the element in the array and value is the value to be assigned.
For example, to assign values to the first elements, you can write the code below:
numbers[0] = 10;
numbers[1] = 5;
numbers[2] = 8;
numbers[3] = 3;
numbers[4] = 7; and so on until numbers[n-1] = value;
Just like the bookshelf analogy, when you want a particular book, you have to go to the shelf where it is located and pick the book from that slot. You also access array elements using their index.
numbers[index];
Note: You will get an ArrayIndexOutOfBoundsException at runtime if you try to access an element outside the bounds of the array (if the index is negative or greater than or equal to the array size).
You can use a loop to access all elements of an array if you know its size.
For example, if the size of the array is n, you can access all elements using a for loop:
for (int i = 0; i < n; i++) {
int variable = numbers[i];
}
You can also use a loop to print all elements of an array horizontally:
for (int i = 0; i < n; i++) {
System.out.print(numbers[i] + " ");
}
If you wish to print the elements in vertical order, you can use println():
for (int i = 0; i < n; i++) {
System.out.println(numbers[i]);
}
In Java, every array has a built-in length property that tells you how many elements it can hold:
int size = numbers.length;
Unlike C, you do not need to divide sizeof() bytes; Java manages array sizes and memory automatically via objects.
You can change the value of an array element by assigning a new value to it via its index:
numbers[index] = new_value;
For example, to change the value of the element at index 2 to 15:
numbers[2] = 15;
Let's have fun by developing a lottery game in Java!
Our lottery game will ask the user to enter how many lotto numbers they want to stake:
The game will keep the user's numbers in an array, generate random lotto numbers, compare them, and compute the user's winnings ($100 per match).
To generate random numbers in Java, you can use the Random class from the java.util package, or Math.random().
Using the Random class:
import java.util.Random;
Random random = new Random();
int random_number = random.nextInt(range) + min;
For example, to generate a random number between 1 and 100:
int random_number = random.nextInt(100) + 1;
import java.util.Scanner;
import java.util.Random;
public class LottoGame {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
System.out.print("Enter the number of lotto numbers you want to stake: ");
int n = scanner.nextInt();
int[] stakes = new int[n]; // Declare the array of size n
// Take each user number with validation (1 to 100)
for (int i = 0; i < n; i++) {
do {
System.out.print("Enter your lotto number " + (i + 1) + ": ");
stakes[i] = scanner.nextInt();
} while ((stakes[i] < 1) || (stakes[i] > 100));
}
// Generate random winning numbers
int[] winning_numbers = new int[n];
for (int i = 0; i < n; i++) {
winning_numbers[i] = random.nextInt(100) + 1;
}
// Loop through the user's stakes to find matches
int matches = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (stakes[i] == winning_numbers[j]) {
matches++;
}
}
}
System.out.println("The winning numbers are:");
for (int i = 0; i < n; i++) {
System.out.println(winning_numbers[i]);
}
double prize_per_winning = 100.0;
double winnings = matches * prize_per_winning;
System.out.printf("Price per winning is $%.2f%n", prize_per_winning);
System.out.printf("You matched %d number(s) and won $%.2f!%n", matches, winnings);
scanner.close();
}
}
We can also put our lotto game inside a loop so the user can play multiple times and enter 0 to exit the game:
import java.util.Scanner;
import java.util.Random;
public class LottoGameLoop {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int exit = 1;
while (exit != 0) {
System.out.print("Enter the number of lotto numbers you want to stake: ");
int n = scanner.nextInt();
int[] stakes = new int[n];
for (int i = 0; i < n; i++) {
do {
System.out.print("Enter your lotto number " + (i + 1) + ": ");
stakes[i] = scanner.nextInt();
} while ((stakes[i] < 1) || (stakes[i] > 100));
}
int[] winning_numbers = new int[n];
for (int i = 0; i < n; i++) {
winning_numbers[i] = random.nextInt(100) + 1;
}
int matches = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (stakes[i] == winning_numbers[j]) {
matches++;
}
}
}
System.out.println("The winning numbers are:");
for (int i = 0; i < n; i++) {
System.out.println(winning_numbers[i]);
}
double prize_per_winning = 100.0;
double winnings = matches * prize_per_winning;
System.out.printf("Price per winning is $%.2f%n", prize_per_winning);
System.out.printf("You matched %d number(s) and won $%.2f!%n", matches, winnings);
System.out.print("Enter 0 to exit or any other number to play again: ");
exit = scanner.nextInt();
}
scanner.close();
}
}