Arrays in Java
In this lesson: Declare, initialise and loop arrays in Java — 1D, 2D, length, for-each and Arrays class — with output. In Hindi & English.
What is an array
An array holds many values of the same type in a single variable, each reached by an index number. Instead of 5 separate variables for 5 marks, one array stores them all. Arrays have a fixed size once created.
Declaring and initialising
int[] marks = {90, 85, 78, 92}; // with values
int[] scores = new int[5]; // size 5, all 0s
String[] names = {"Aman", "Diya"}; // string array
You can create an array directly with values in { }, or make an empty one of a fixed size with new int[5]. The type has square brackets: int[].
Accessing elements
int[] marks = {90, 85, 78, 92};
System.out.println(marks[0]); // first -> 90
System.out.println(marks[3]); // fourth -> 92
System.out.println(marks.length); // size -> 4
marks[1] = 88; // change second
92
4
Use the index in square brackets to read or change an element. marks.length gives the size (a field, no brackets). Remember the first element is index 0.
Looping over an array
int[] marks = {90, 85, 78, 92};
for (int i = 0; i < marks.length; i++) {
System.out.println(marks[i]);
}
85
78
92
A for loop from 0 to length - 1 visits every element. Using marks.length as the limit keeps it correct even if the size changes.
The for-each loop
int[] marks = {90, 85, 78, 92};
for (int m : marks) {
System.out.println(m);
}
85
78
92
The for-each loop reads each value directly into m — no index needed. It is cleaner when you just want to go through all values in order.
2D arrays
int[][] grid = {
{1, 2, 3},
{4, 5, 6}
};
System.out.println(grid[1][2]); // row 1, col 2 -> 6
A 2D array is an array of arrays — like a table with rows and columns. You access an element with two indexes: grid[row][col].
Common mistakes
- Index out of bounds. Valid indexes are 0 to
length - 1; anything else throws an error. - Using
length()on an array. Arrays uselength(a field), Strings uselength(). - Forgetting arrays are fixed size. To grow, use an
ArrayListinstead. - Starting the loop at 1. Start at index 0, or you miss the first element.
Summary
- An array holds many same-type values, accessed by index.
- Indexes start at 0; the last is
length - 1. - Use
arr.length(a field) for the size. - Loop with a
for(index) or afor-each(values). - 2D arrays are arrays of arrays, accessed as
arr[row][col].
Next, use a flexible list with ArrayList vs LinkedList, or loop over arrays in Java loops.
Frequently Asked Questions
What is an array in Java?
How do I find the size of an array?
length field: arr.length. Note it is a field, not a method — there are no brackets, unlike String's length().Why does array indexing start at 0?
length - 1.What is the difference between a for loop and a for-each loop?
for loop uses an index, so you can change elements and know their position. A for-each loop reads each element directly, which is simpler when you only need to read values in order.What is an ArrayIndexOutOfBoundsException?
length - 1.