Ever tried to line up your school notebooks in order? That's what an array does for data.
💡 In Simple Words: An array is a single box that holds many tiny boxes, each with the same kind of thing, like a row of numbered lockers. You can grab any locker by its number.
What is an Array in Java?
In Java, an array (a fixed‑size list) stores a collection of elements that are all the same type. Think of it as a train with a set number of carriages; each carriage holds one passenger, and you know exactly how many carriages there are.
Declaration and Creation
First you tell Java what kind of thing you want to store, then you ask for a certain number of slots.
int[] marks; // declaration – says "I will have an array of integers"
marks = new int[5]; // creation – asks Java for 5 slots
You can combine both steps:
int[] marks = new int[5];
Initialization – putting values inside
After the array exists, you can fill each slot, just like putting books on each shelf.
marks[0] = 78; // first slot (index 0)
marks[1] = 85;
marks[2] = 92;
marks[3] = 67;
marks[4] = 88;
Or you can do it all at once:
int[] marks = {78, 85, 92, 67, 88};
Accessing Elements
Use the index (the locker number) inside square brackets.
int highest = marks[2]; // gets the value 92
Remember, Java starts counting at 0, so the last element of a 5‑slot array is at index 4.
Array Length
Every array knows how many slots it has. The length property gives you that number.
int total = marks.length; // total is 5
Why Use Arrays?
- Save time – you don’t need a separate variable for each value.
- Easy to loop – you can run the same code for every element.
- Predictable size – the number of slots never changes, which helps the computer manage memory.
Common Operations
Below is a quick cheat‑sheet of things you’ll do with arrays in exams.
| Operation | Code Example | What it does |
|---|---|---|
| Declare | int[] a; | Tell Java you will store integers. |
| Create | a = new int[3]; | Allocate three slots. |
| Initialize | a = {1,2,3}; | Fill slots with values. |
| Access | int x = a[1]; | Read the second value (2). |
| Modify | a[2] = 10; | Change third value to 10. |
| Length | a.length | Get number of slots (3). |
| Loop | for(int i=0;iPrint every element. |
Working with Arrays: A Sample Question
Suppose the exam asks you to store the scores of five subjects and print the average.
public class AvgScore {
public static void main(String[] args) {
int[] score = {78, 85, 92, 67, 88};
int sum = 0;
for(int i = 0; i
Key points to notice:
- We used
int[] score = {…}to both create and initialize. - The
forloop runs from 0 toscore.length‑1, ensuring every element is visited. - We cast
sumtodoubleso the division gives a decimal result.
Tips for ICSE Exams
- Always write the index range correctly: 0 to
array.length‑1. - Remember that arrays cannot change size after creation – if you need more slots, you must create a new larger array.
- When printing, use
System.out.printlninside the loop for each element, or build a single string first. - Watch out for ArrayIndexOutOfBoundsException – that fancy term just means you tried to reach a locker that doesn't exist.
📝 Likely Exam Questions
- Write a Java program to store the names of three friends in a String array and display them.
String[] friends = {"Asha", "Rohit", "Meena"}; for(int i=0;i - Explain what happens if you try to access
arr[5]in an array declared asint[] arr = new int[5];
You will get anArrayIndexOutOfBoundsExceptionbecause valid indices are 0‑4. - Given
int[] a = {2,4,6,8};, write the output of the following code:
Answer: 20int sum = 0; for(int i=0;i - How do you find the number of elements stored in an array called
data?
Usedata.length. - What is the difference between declaration and initialization of an array?
Declaration tells Java the type and name (e.g.,int[] x;). Initialization actually creates the slots and may assign values (e.g.,x = new int[3];orx = {1,2,3};).