Java Arrays
Storing multiple values in a single variable
📚 What are Arrays?
Arrays in Java store multiple values of the same type in a single variable. Think of an array as a container with numbered slots, where each slot holds one value that you can access using its position number.
// Create an array of 5 numbers
int[] numbers = {10, 20, 30, 40, 50};
System.out.println("First number: " + numbers[0]);
System.out.println("Third number: " + numbers[2]);
Output:
First number: 10 Third number: 30
Array Concepts
Container
Holds multiple values together
int[] ages = {25, 30, 35};
Index
Position starts from 0
ages[0] // first item
Length
Fixed size when created
ages.length // size
Same Type
All elements same data type
String[] names;
🔹 Creating Arrays
There are different ways to create arrays in Java:
🔸 Method 1: Declare and Initialize
// Create array with values
int[] scores = {85, 92, 78, 96, 88};
String[] fruits = {"apple", "banana", "orange"};
System.out.println("First score: " + scores[0]);
System.out.println("Second fruit: " + fruits[1]);
Output:
First score: 85 Second fruit: banana
🔸 Method 2: Declare Then Assign
// Create empty array first
int[] numbers = new int[3]; // Array of size 3
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
System.out.println("Array created and filled:");
for (int i = 0; i < numbers.length; i++) {
System.out.println("numbers[" + i + "] = " + numbers[i]);
}
Output:
Array created and filled: numbers[0] = 10 numbers[1] = 20 numbers[2] = 30
🔹 Accessing Array Elements
Use square brackets with index number (starting from 0):
String[] colors = {"red", "green", "blue", "yellow"};
// Access elements by index
System.out.println("First color: " + colors[0]);
System.out.println("Last color: " + colors[3]);
// Array length
System.out.println("Total colors: " + colors.length);
// Change an element
colors[1] = "purple";
System.out.println("Changed second color to: " + colors[1]);
Output:
First color: red Last color: yellow Total colors: 4 Changed second color to: purple
🔹 Looping Through Arrays
Use loops to process all array elements:
🔸 Regular For Loop
int[] grades = {88, 92, 76, 95, 83};
System.out.println("All grades:");
for (int i = 0; i < grades.length; i++) {
System.out.println("Student " + (i+1) + ": " + grades[i]);
}
Output:
All grades: Student 1: 88 Student 2: 92 Student 3: 76 Student 4: 95 Student 5: 83
🔸 Enhanced For Loop (For-Each)
String[] animals = {"cat", "dog", "bird", "fish"};
System.out.println("Pet animals:");
for (String animal : animals) {
System.out.println("- " + animal);
}
Output:
Pet animals: - cat - dog - bird - fish
🔹 Practical Array Examples
🔸 Find Maximum Value
int[] temperatures = {23, 31, 18, 27, 35, 22};
int max = temperatures[0]; // Start with first value
for (int i = 1; i < temperatures.length; i++) {
if (temperatures[i] > max) {
max = temperatures[i];
}
}
System.out.println("Highest temperature: " + max + "°C");
Output:
Highest temperature: 35°C
🔸 Calculate Average
double[] prices = {12.99, 8.50, 15.75, 22.00, 9.25};
double sum = 0;
// Add all prices
for (double price : prices) {
sum += price;
}
double average = sum / prices.length;
System.out.println("Average price: $" + String.format("%.2f", average));
Output:
Average price: $13.70