Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 13

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.

💡 Indexes start at 0, so an array of size 5 has indexes 0, 1, 2, 3, 4.

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
90
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]);
}
90
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);
}
90
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
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 use length (a field), Strings use length().
  • Forgetting arrays are fixed size. To grow, use an ArrayList instead.
  • 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 a for-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?
An array is a container that holds many values of the same type in one variable, accessed by an index number. For example, one array can hold 100 marks instead of 100 separate variables. Its size is fixed once created.
How do I find the size of an array?
Use the 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?
The first element is at index 0, the second at 1, and so on. So an array of size 5 has valid indexes 0 to 4. The last index is always length - 1.
What is the difference between a for loop and a for-each loop?
A normal 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?
It is the error you get when you use an index outside the array, like index 5 in a size-5 array (valid indexes are 0 to 4). Always keep indexes between 0 and length - 1.
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicStrings in Java Your progress: 13 / 61 (21%) · Lesson 14 / 61 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free