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:
- The first value is at index 0, not 1.
- The last value is at index 4, which is size minus 1.
- 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.
- Find the sum of all elements in an array
- Find the largest element in an array
- Reverse an array in place
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?
Why does the array index start at 0?
Can an array store different data types?
Can the size of an array change?
What is ArrayIndexOutOfBoundsException?
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

