Java Array Basics: Learn Arrays, Indexing, Length & Common Mistakes

0

Imagine you need to store the marks of 50 students. Creating 50 separate variables such as mark1, mark2, and mark3 would quickly become difficult to manage. Java provides arrays to solve this problem. An array allows multiple values of the same data type to be stored under a single variable name.

What Is an Array?

An array is a fixed-size collection of elements having the same data type. Each element occupies a position identified by an index. Java uses zero-based indexing, which means the first element is always at index 0.

Important: The size of a Java array is fixed after creation. You cannot directly increase or decrease its length.

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

The marks array contains five integer values. The first value is stored at index 0, while the last value is stored at index 4.

Index Value Meaning
0 85 First element
1 90 Second element
2 78 Third element
3 92 Fourth element
4 88 Fifth element

Why Do We Need Arrays?

Arrays become useful whenever a program needs to work with a group of related values. Instead of creating many individual variables, one array can represent the complete collection.

int mark1 = 85;
int mark2 = 90;
int mark3 = 78;

The same information can be represented much more cleanly with an array:

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

This becomes especially valuable when the number of values is large. Arrays also work naturally with loops, making it easy to process every element using a small amount of code.

Accessing Array Elements

You can access an individual array element by using its index inside square brackets.

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

System.out.println(marks[0]);
System.out.println(marks[3]);

The first statement prints 85 because index 0 contains 85. The second statement prints 92 because 92 is stored at index 3.

Remember: If an array contains 5 elements, its valid indexes are 0, 1, 2, 3, and 4. The array length is 5, but the last index is 4.

Changing an Array Element

Array elements are not read-only. After creating an array, you can replace an existing value by assigning a new value to its index.

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

marks[1] = 95;

System.out.println(marks[1]);

Initially, index 1 contains 90. After the assignment, that value is replaced with 95.

Finding the Length of an Array

Java provides the length property to determine how many elements an array can contain.

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

System.out.println(marks.length);

The output is 5 because the array contains five elements.

Beginner Tip: Array length and the last index are different. For an array of length 5, the last valid index is length - 1, which is 4.

Array Index Out of Bounds

One of the most common beginner mistakes is trying to access an index that does not exist.

int[] numbers = {10, 20, 30};

System.out.println(numbers[3]);

This code throws ArrayIndexOutOfBoundsException because the valid indexes are only 0, 1, and 2. Index 3 is outside the array.

Real-World Example

Consider a small application that stores the monthly sales of a shop. Instead of creating twelve separate variables, an array can represent all twelve months.

int[] monthlySales = {12000, 15000, 13500, 18000, 21000, 19500};

System.out.println(monthlySales[0]);
System.out.println(monthlySales[5]);

The same idea appears throughout software development. Arrays are useful for storing scores, prices, IDs, measurements, sensor readings, product quantities, and many other collections of related data.

Common Beginner Mistakes

  • Assuming array indexing starts from 1 instead of 0.
  • Trying to access an index equal to the array length.
  • Expecting an array to automatically grow when new elements are added.
  • Confusing the length property of an array with a method.
  • Assuming an array can contain different primitive data types in the same collection.

Quick Learning Check

Suppose you have an array containing 10 elements. What is the first index? What is the last valid index? The answers are 0 and 9. This simple relationship—last index equals length - 1—will save you from many indexing errors later.

Interview Insight

A common Java interview question is: “What is an array?” A strong answer is: “An array is a fixed-size, indexed collection that stores elements of the same type. Its length is determined when the array is created, and indexing starts from zero.”

Concept Key Point
Array Stores multiple values of the same type
Index Identifies an element and starts from 0
Length Number of elements the array can contain
Last Index Always length - 1
Size Fixed after array creation
Invalid Index Can cause ArrayIndexOutOfBoundsException

Arrays are one of the fundamental building blocks of Java programming. Once you understand that an array is a fixed-size collection accessed through zero-based indexes, many later concepts—including loops, searching, sorting, multidimensional data, and the Java Arrays utility class—become much easier to understand.

Post a Comment

0Comments
Post a Comment (0)