Ever wondered how a Java program stores a list of scores without writing dozens of variables?
Arrays are like a row of lockers that all hold the same kind of thing. You give each locker a number, then you can put a value in or take it out whenever you need.
What are Arrays in Java?
An array is a collection of items of the same type, stored in contiguous memory slots. Think of it like a row of lockers, each locker has the same size and a number on the door. You can open locker 0, locker 1, and so on, to put or take a book.
How to Declare and Initialize an Array
First you tell Java what kind of data the lockers will hold. Then you give the row a name. The basic syntax looks like this:
int[] marks = new int[5];
Here int means whole numbers, [] tells Java we want an array, marks is the name, and new int[5] creates five empty lockers.
You can also fill the lockers right away:
int[] marks = {85, 92, 78, 90, 88};
The numbers inside the curly braces are the initial values for locker 0 through locker 4.
Key points to remember
- The index (the locker number) always starts at 0.
- All elements must be of the same data type.
- Once the size is set, you cannot change it.
Common Array Operations
Below are the most useful things you’ll need for the ICSE exam.
Reading an element
int firstScore = marks[0]; // gets the value in the first locker
Changing an element
marks[2] = 80; // replaces the third value with 80
Finding the length
int size = marks.length; // returns 5 for the example above
Looping through an array
Loops let you visit each locker without writing the same line over and over.
for (int i = 0; i
Or you can use the enhanced for‑loop (sometimes called a “for‑each” loop) which reads a bit like English:
for (int score : marks) {
System.out.println(score);
}
Array vs. ArrayList – Quick Comparison
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed once created | Grows or shrinks automatically |
| Data type | Primitive or object | Only objects (use wrapper classes for primitives) |
| Syntax simplicity | Simple, less code for fixed size | More methods, a bit longer |
| Performance | Very fast for fixed‑size access | Slightly slower due to dynamic resizing |
Tips for Solving ICSE Array Questions
- Write the index numbers on the side of your paper. It helps avoid “off‑by‑one” mistakes.
- Remember that
.lengthis a property, not a method. No parentheses. - If the question asks for the sum of all elements, use a loop and a running total variable.
- When the problem involves two‑dimensional data (like a matrix), treat it as an array of arrays.
- Practice converting a problem statement into array code before you start typing.
Sample Question with Solution
Question: Write a program that reads five integers, stores them in an array, and prints the highest value.
import java.util.Scanner;
public class MaxScore {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] scores = new int[5];
for (int i = 0; i max) {
max = s;
}
}
System.out.println("Highest score = " + max);
}
}
📝 Likely Exam Questions
- Explain how to declare an integer array of size 10 and initialize it with values 1 to 10.
Answer:int[] arr = {1,2,3,4,5,6,7,8,9,10}; - What will be the output of the following code?
Answer: 4int[] a = {2,4,6}; System.out.println(a[1]); - Write a loop that calculates the sum of all elements in an array called
marks.
Answer:int sum = 0; for (int m : marks) { sum += m; } - How does the
.lengthproperty of an array differ from thesize()method of anArrayList?
Answer:.lengthis a fixed property of an array, no parentheses;size()is a method that returns the current number of elements in an ArrayList. - Given
int[] nums = new int[3];, what are the default values stored in each element?
Answer: 0 for all three positions.