← Back to DSA Course | Chapter 2: Arrays | Lesson 1 of 8

Array Introduction

What is an Array?

An array is a fixed-size collection that stores multiple values of the same type in contiguous memory, so each value can be found instantly using its position. The first index is 0 in most languages, meaning an array of 5 elements has valid indexes from 0 through 4.

Example: What is an Array?

#include <iostream>
using namespace std;
int main() {
    int scores[4] = {90, 85, 78, 92}; // fixed-size, same type, contiguous
    cout << "First element (index 0): " << scores[0] << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] scores = {90, 85, 78, 92}; // fixed-size, same type, contiguous
        System.out.println("First element (index 0): " + scores[0]);
    }
}
scores = [90, 85, 78, 92]  # a list; conceptually contiguous, indexed from 0
print("First element (index 0):", scores[0])
#include <stdio.h>
int main() {
    int scores[4] = {90, 85, 78, 92}; /* fixed-size, same type, contiguous */
    printf("First element (index 0): %d\n", scores[0]);
    return 0;
}

Array Declaration

Declaring an array tells the program both the type of value it will hold and how many elements it needs to reserve space for upfront. Because that size is fixed at creation for a normal array, resizing it later usually means creating a brand-new, larger array and copying the old data over.

Example: Array Declaration

#include <iostream>
using namespace std;
int main() {
    int numbers[5]; // reserves space for 5 ints upfront, size fixed at creation
    for (int i = 0; i < 5; i++) numbers[i] = i * 10;
    cout << "numbers[3]: " << numbers[3] << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] numbers = new int[5]; // reserves space for 5 ints, size fixed at creation
        for (int i = 0; i < 5; i++) numbers[i] = i * 10;
        System.out.println("numbers[3]: " + numbers[3]);
    }
}
numbers = [0] * 5  # pre-sized list (Python lists can still grow, unlike a real array)
for i in range(5):
    numbers[i] = i * 10
print("numbers[3]:", numbers[3])
#include <stdio.h>
int main() {
    int numbers[5]; /* reserves space for 5 ints, size fixed at creation */
    for (int i = 0; i < 5; i++) numbers[i] = i * 10;
    printf("numbers[3]: %d\n", numbers[3]);
    return 0;
}

Indexing

An index is the numeric position used to access a specific element, and because arrays store elements contiguously in memory, jumping directly to any valid index is an O(1) operation. Accessing an index outside the valid range (0 to size−1) causes an out-of-bounds error.

Example: Indexing

#include <iostream>
using namespace std;
int main() {
    int arr[] = {100, 200, 300, 400};
    cout << "arr[0]: " << arr[0] << endl;
    cout << "arr[2]: " << arr[2] << " (O(1) direct jump)" << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {100, 200, 300, 400};
        System.out.println("arr[0]: " + arr[0]);
        System.out.println("arr[2]: " + arr[2] + " (O(1) direct jump)");
    }
}
arr = [100, 200, 300, 400]
print("arr[0]:", arr[0])
print("arr[2]:", arr[2], "(O(1) direct jump)")
#include <stdio.h>
int main() {
    int arr[] = {100, 200, 300, 400};
    printf("arr[0]: %d\n", arr[0]);
    printf("arr[2]: %d (O(1) direct jump)\n", arr[2]);
    return 0;
}

Array Size

Knowing an array's size upfront is essential for writing correct loops: it tells you exactly where to stop iterating and helps you avoid accidentally reading or writing past the last valid element, which is one of the most common bugs beginners hit.

Example: Array Size

#include <iostream>
using namespace std;
int main() {
    int arr[] = {4, 8, 15, 16, 23};
    int size = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < size; i++) cout << arr[i] << " "; // stop exactly at size
    cout << endl << "Size: " << size << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] arr = {4, 8, 15, 16, 23};
        int size = arr.length;
        for (int i = 0; i < size; i++) System.out.print(arr[i] + " ");
        System.out.println("\nSize: " + size);
    }
}
arr = [4, 8, 15, 16, 23]
size = len(arr)
for i in range(size):
    print(arr[i], end=" ")
print("\nSize:", size)
#include <stdio.h>
int main() {
    int arr[] = {4, 8, 15, 16, 23};
    int size = sizeof(arr) / sizeof(arr[0]);
    for (int i = 0; i < size; i++) printf("%d ", arr[i]);
    printf("\nSize: %d\n", size);
    return 0;
}

Basic Array Practice

Arrays are the natural fit for storing lists of similar values, like a class's exam marks, a shopping cart's prices, or a set of ages. Starting with simple tasks like finding the maximum value or computing a total builds the muscle memory you'll need for more advanced array algorithms.

Example: Basic Array Practice

#include <iostream>
using namespace std;
int main() {
    int marks[] = {88, 92, 75, 60, 99};
    int n = 5, maxVal = marks[0], total = 0;
    for (int i = 0; i < n; i++) {
        total += marks[i];
        if (marks[i] > maxVal) maxVal = marks[i];
    }
    cout << "Max: " << maxVal << ", Total: " << total << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        int[] marks = {88, 92, 75, 60, 99};
        int maxVal = marks[0], total = 0;
        for (int m : marks) {
            total += m;
            if (m > maxVal) maxVal = m;
        }
        System.out.println("Max: " + maxVal + ", Total: " + total);
    }
}
marks = [88, 92, 75, 60, 99]
print("Max:", max(marks), ", Total:", sum(marks))
#include <stdio.h>
int main() {
    int marks[] = {88, 92, 75, 60, 99};
    int n = 5, maxVal = marks[0], total = 0;
    for (int i = 0; i < n; i++) {
        total += marks[i];
        if (marks[i] > maxVal) maxVal = marks[i];
    }
    printf("Max: %d, Total: %d\n", maxVal, total);
    return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.