Programming BasicsBasicsbeginner

What Is an Array? Beginner Guide with Examples

Arrays explained in plain words first: index, size, reading, updating and looping, then the same ideas in Java and Python.

Published
TL;DR – Quick Answer

An array is a collection of values stored under one name, where each value has a position number called an index. In most languages the first index is 0, so an array of size 5 has indexes 0 to 4. You read or change a value by its index and visit all values with a loop.

On This Page

An array is a way to store many values under one name. Instead of making 50 separate variables for 50 students' marks, you make one array that holds all 50. Arrays are the first data structure every programmer learns, and almost every program uses them.

This page explains arrays in plain words first, with no code. Then it shows the same ideas in Java and Python.

The idea: a row of numbered boxes

Picture the pigeon-hole letter boxes in a hostel office. They sit in one row, each box has a number painted on it, and each box holds one thing. To pick up a letter from box 3, you go straight to box 3. You do not open boxes 0, 1 and 2 first.

An array works the same way:

  • The whole row has one name, such as marks.
  • Each box holds one value.
  • Each box has a number, called the index.
  • The size (or length) is the number of boxes.

Here is an array called marks with five values:

Index 0 1 2 3 4
Value 78 85 62 90 71

Read this table carefully, because three facts follow from it:

  1. The first value is at index 0, not 1.
  2. The last value is at index 4, which is size minus 1.
  3. There is no index 5. Asking for it is an error.

Index: why counting starts at 0

The index means "how many steps from the start". The first box is zero steps from the start, so it is index 0. The second box is one step away, so it is index 1.

This feels strange at first. It becomes natural after a week of practice. C, C++, Java, Python and JavaScript all start at 0.

Position in plain words Index
1st value 0
2nd value 1
3rd value 2
nth value n - 1
Last value size - 1

The four things you do with an array

These steps are the same in every language. Only the spelling changes.

Action Plain words Pseudocode
Create Make 5 boxes with these values marks = [78, 85, 62, 90, 71]
Read What is in box 2? marks[2] gives 62
Update Put 70 in box 2 marks[2] = 70
Loop Visit every box from first to last for each index i from 0 to size - 1

Now let us write these steps in real code.

Arrays in Java

Java arrays have a fixed size and hold one type of value.

public class ArrayBasics {
    public static void main(String[] args) {
        int[] marks = {78, 85, 62, 90, 71};

        System.out.println("Size: " + marks.length);
        System.out.println("First: " + marks[0]);
        System.out.println("Last: " + marks[marks.length - 1]);

        marks[2] = 70;
        System.out.println("After update, index 2: " + marks[2]);
    }
}

Output:

Size: 5
First: 78
Last: 71
After update, index 2: 70

int[] means "an array of int". marks.length gives the size. Note that length has no brackets for arrays.

You can also create an empty array of a given size. Java fills it with default values: 0 for numbers, false for boolean and null for objects.

public class EmptyArray {
    public static void main(String[] args) {
        int[] scores = new int[3];
        scores[0] = 45;
        System.out.println(scores[0] + " " + scores[1] + " " + scores[2]);
    }
}

Output:

45 0 0

Arrays in Python

Python does not have a built-in array type for everyday use. It uses a list, which works like an array that can grow.

marks = [78, 85, 62, 90, 71]

print("Size:", len(marks))
print("First:", marks[0])
print("Last:", marks[-1])

marks[2] = 70
print("After update, index 2:", marks[2])

marks.append(88)
print("After append:", marks)

Output:

Size: 5
First: 78
Last: 71
After update, index 2: 70
After append: [78, 85, 70, 90, 71, 88]

Two Python extras: marks[-1] gives the last value, and append adds a value at the end. Neither works on a plain Java array.

Looping through an array

Arrays and loops work together. One loop can visit every value, whether the array has 5 values or 5 lakh.

This program finds the total, average and highest mark.

Java

public class ArrayLoop {
    public static void main(String[] args) {
        int[] marks = {78, 85, 62, 90, 71};
        int total = 0;
        int highest = marks[0];

        for (int i = 0; i < marks.length; i++) {
            total += marks[i];
            if (marks[i] > highest) {
                highest = marks[i];
            }
        }

        System.out.println("Total: " + total);
        System.out.println("Average: " + (total / (double) marks.length));
        System.out.println("Highest: " + highest);
    }
}

Python

marks = [78, 85, 62, 90, 71]
total = 0
highest = marks[0]

for mark in marks:
    total += mark
    if mark > highest:
        highest = mark

print("Total:", total)
print("Average:", total / len(marks))
print("Highest:", highest)

Output (same for both):

Total: 386
Average: 77.2
Highest: 90

Trace of the Java loop:

i marks[i] total highest
0 78 78 78
1 85 163 85
2 62 225 85
3 90 315 90
4 71 386 90

The Java loop condition is i < marks.length, not <=. With <=, the last round would try marks[5], which does not exist. Java also has an enhanced for loop, for (int m : marks), which visits each value without an index.

What goes wrong: index out of range

Using an index that does not exist is the most common array bug.

public class OutOfBounds {
    public static void main(String[] args) {
        int[] marks = {78, 85, 62};
        System.out.println(marks[3]);
    }
}

Java stops the program with this error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

Python gives IndexError: list index out of range for the same mistake. C does not check at all. It quietly reads whatever memory comes next, which can give wrong answers or crash later. This is why careful index handling matters so much in C.

Where arrays are fast and where they are slow

Task Speed Why
Read or update by index Very fast The computer jumps straight to the box
Add at the end (Python list) Usually fast There is often spare room at the end
Insert or delete in the middle Slow on big arrays Every value after that point must shift
Search for a value in an unsorted array Slow on big arrays You may have to check every box

When you need many inserts in the middle, a linked list can be better. The array vs linked list page explains the trade-off, and what are data structures shows where arrays fit among the others.

Practice problems on arrays

Start with these. Each one uses the read, update and loop steps from this page.

Try each one in plain words first. Write the steps, trace them on a small array in a table like the one above, and only then write code. For more on Java arrays, including 2D arrays, read arrays in Java.

Frequently Asked Questions

What is an array in simple words?
It is a row of numbered boxes that share one name. Each box holds one value, and you reach a box by its number, called the index.
Why does the array index start at 0?
The index tells how far a value is from the start of the array. The first value is 0 steps away, so its index is 0. C, Java, Python and JavaScript all count from 0.
Can an array store different data types?
In Java and C, all values in an array must be the same type. A Python list can mix types, but keeping one type per list makes your code easier to understand.
Can the size of an array change?
A Java or C array has a fixed size once it is created. To grow, use ArrayList in Java. A Python list grows and shrinks as you add or remove items.
What is ArrayIndexOutOfBoundsException?
It is the error Java throws when you use an index below 0 or equal to or greater than the length. For an array of length 5, index 5 is already outside. Python raises IndexError in the same situation.

Want to Build Your Career in Java Full Stack with AI?

Join CodeBegun and train with working industry engineers — See the Java Full Stack course in Hyderabad

Apply for Demo Class →
Siva Prasad Galaba
Founder, CodeBegun · Staff Engineer

Founder of CodeBegun. 15+ years building Java systems at companies like Crunchyroll. Teaches Java, Spring Boot and system design the way the industry actually works, and mentors students through projects, mock interviews and placement preparation.

Technically reviewed by CodeBegun Technical TeamLast reviewed 4 December 2026 LinkedIn
Chat with us