Why bother with arrays?

Imagine you have a row of lockers, each holding a single notebook. Instead of naming each locker separately, you give the whole row one name and refer to each locker by its number. In Java, an array works just like that row of lockers – a neat way to store many values of the same type under one variable.

💡 In Simple Words: An array is a collection of items, all of the same kind, stored together. You give the collection a name, and you pick any item by its position (starting from zero).

What is an Array in Java?

An array is a fixed-size container that holds a series of elements that are all of the same data type (like int, char, or String). The size of the array is decided when you create it and cannot change later.

Key terms

  • Element – one value stored inside the array, like a single locker’s notebook.
  • Index – the position number of an element, starting at 0 for the first slot.
  • Length – the total number of slots the array has.

How to Declare and Initialise an Array

There are two steps: declare the variable that will hold the array, and allocate memory for the actual elements.

// Declaration
int[] marks;
// Allocation (creates space for 5 integers)
marks = new int[5];

You can combine both steps in one line:

int[] marks = new int[5];

If you already know the values, you can initialise them right away:

int[] marks = {85, 90, 78, 92, 88};

Accessing and Modifying Array Elements

Use the index inside square brackets to read or change a value.

int first = marks[0]; // reads 85
marks[2] = 80;      // changes the third element from 78 to 80

Remember, trying to use an index that’s outside the range (like marks[5]ArrayIndexOutOfBoundsException – a fancy way of saying “you’re looking at a locker that doesn’t exist”.

Common Array Operations

  • Traversing – visiting each element, usually with a for loop.
    for (int i = 0; i 
  • Finding the sum of numeric arrays.
    int sum = 0;
    for (int val : marks) {
        sum += val;
    }
    
  • Finding the maximum value.
    int max = marks[0];
    for (int i = 1; i  max) max = marks[i];
    }
    

Array vs. ArrayList – Quick Comparison

FeatureArrayArrayList (java.util)
SizeFixed after creationDynamic – grows as needed
Data typeCan hold primitives (int, char) directlyHolds objects only (use wrapper classes for primitives)
Syntax simplicitySimple, especially for fixed‑size dataMore methods, but flexible
PerformanceVery fast for indexed accessSlight overhead due to resizing

Step‑by‑Step: Creating and Using an Array

graph TD A[Declare array variable] --> B[Allocate memory with new] B --> C[Optionally initialise values] C --> D[Access/modify elements] D --> E[Iterate with loops] E --> F[Use in program logic]

Common Pitfalls to Avoid

  • Mixing up the size (length) with the highest index – the last index is length‑1.
  • Forgetting that arrays of objects store references, not the objects themselves.
  • Using == to compare String array elements – use .equals() instead.

📝 Likely Exam Questions

  1. Write a Java program to store the marks of five students in an array and display the highest mark.
    public class HighestMark {
        public static void main(String[] args) {
            int[] marks = {85, 90, 78, 92, 88};
            int max = marks[0];
            for (int i = 1; i  max) max = marks[i];
            }
            System.out.println("Highest mark = " + max);
        }
    }
    
  2. Explain what happens if you try to access arr[10] in an array declared as int[] arr = new int[5];.
    Answer: Java throws an ArrayIndexOutOfBoundsException because index 10 is outside the valid range 0‑4.
  3. Differentiate between an array and an ArrayList in two points.
    Answer: (i) Array size is fixed at creation; ArrayList can grow dynamically. (ii) Arrays can store primitive types directly; ArrayList stores only objects, so primitives need wrapper classes.
  4. Given the array int[] a = {2,4,6,8};, write the output of the following loop:
    for (int i = a.length-1; i >= 0; i--) {
          System.out.print(a[i] + " ");
      }
    
    Answer: 8 6 4 2 (the array is printed in reverse order).
#ICSE#Class 10#Java#Arrays#Computer Applications