Java Arrays
Store related values in a fixed-size sequence.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Arrays.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
An array has a fixed length after creation. Elements use zero-based indexes, so valid indexes range from 0 through length – 1. Changing one element is different from resizing the array.
Explore the details
Java Arrays
An array stores multiple values of the same type in a single variable — like a row of numbered boxes, each holding one item.
Instead of writing String car1 = "Volvo", String car2 = "BMW"… you store them all in one array and access each by its index.
Declaring and Initialising an Array
Code anatomy / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
String | The type of every element in the array |
[] | Square brackets declare this as an array |
cars | The array variable name |
{ | Opening brace — list your values inside |
"Volvo" | Element at index 0 |
"BMW" | Element at index 1 |
"Ford" | Element at index 2 |
} | Closing brace |
Loop Through an Array
Use a for loop with .length to visit every element:
Index starts at 0, not 1. An array of 4 elements has indexes 0, 1, 2, 3. Accessing index 4 throws an ArrayIndexOutOfBoundsException. Use .length - 1 to safely reach the last element.
int[] nums = {10, 20, 30, 40, 50};
System.out.println(nums.length); // 5
System.out.println(nums[2]); // 30
nums[2] = 99;
// for-each traversal
for (int n : nums) System.out.print(n + " "); // 10 20 99 40 50
// Sort
java.util.Arrays.sort(nums);
System.out.println(java.util.Arrays.toString(nums)); // [10, 20, 40, 50, 99]
// 2D array
int[][] grid = {{1,2,3},{4,5,6},{7,8,9}};
System.out.println(grid[1][2]); // 6A complete runnable example
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
public class Main {
public static void main(String[] args) throws Exception {
int[] scores = {10, 20, 30};
scores[1] = 25;
System.out.println(scores.length);
for (int score : scores) {
System.out.println(score);
}
}
}Predict first. Then reveal the expected output / வெளியீடு
3 10 25 30
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
Make it your own
Practice challenge
Add a fourth value to the initializer and run the same loop.
Reveal the answer and reasoning / விடை
The length becomes 4 and the for-each loop visits all four values.
One mistake to avoid: The last valid index is length – 1. Accessing scores[scores.length] throws an exception.
Your learning checkpoint
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.