# Arrays in Java — 1D, 2D, the Arrays Class and Common Programs

Source: https://skills101.in/learn/java/java-arrays — Skills101 free tutorial (Java Programming). Updated 2026-11-09.

An **array** stores a fixed number of values of the **same type** in a row. Its size is set when it is created and can't change.

![Stack vs heap: the array variable lives on the stack and holds a reference to the actual array object on the heap](https://skills101.in/learn-img/java/stack-heap.svg)

(An array is an **object**: the variable holds a reference to it on the heap. That's why copying the variable doesn't copy the array.)

## 1. Create and use

```java
public class Main {
    public static void main(String[] args) {
        int[] marks = {78, 92, 65, 88};       // values known
        int[] scores = new int[3];            // size 3, filled with 0
        scores[0] = 50;
        System.out.println(marks[1] + " " + marks.length + " " + scores[2]);
        for (int i = 0; i < marks.length; i++) {
            System.out.print(marks[i] + " ");
        }
        System.out.println();
    }
}
```

```text
92 4 0
78 92 65 88
```

- Indexes run from **0** to **length − 1**.
- `marks[4]` here throws **ArrayIndexOutOfBoundsException**.
- Default values: 0 for numbers, `false` for boolean, `null` for objects.

## 2. The Arrays helper class

```java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] nums = {42, 7, 19, 3};
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        System.out.println(Arrays.binarySearch(nums, 19));
        int[] copy = Arrays.copyOf(nums, 6);
        System.out.println(Arrays.toString(copy));
        int[] filled = new int[3];
        Arrays.fill(filled, 9);
        System.out.println(Arrays.toString(filled));
    }
}
```

```text
[3, 7, 19, 42]
2
[3, 7, 19, 42, 0, 0]
[9, 9, 9]
```

> `System.out.println(nums)` prints something like `[I@1b6d3586` (a reference), not the values. Use `Arrays.toString(nums)`.

## 3. Copying: reference vs real copy

```java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3};
        int[] b = a;                 // same array
        int[] c = a.clone();         // real copy
        a[0] = 100;
        System.out.println(Arrays.toString(b) + " " + Arrays.toString(c));
    }
}
```

```text
[100, 2, 3] [1, 2, 3]
```

## 4. 2D arrays

```java
public class Main {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2, 3},
            {4, 5, 6}
        };
        System.out.println(grid[1][2] + " rows=" + grid.length + " cols=" + grid[0].length);
        for (int[] row : grid) {
            int sum = 0;
            for (int v : row) sum += v;
            System.out.println(sum);
        }
    }
}
```

```text
6 rows=2 cols=3
6
15
```

## 5. Interview programs

### Largest, smallest and average

```java
public class Main {
    public static void main(String[] args) {
        int[] a = {12, 45, 7, 30};
        int max = a[0], min = a[0], sum = 0;
        for (int x : a) {
            max = Math.max(max, x);
            min = Math.min(min, x);
            sum += x;
        }
        System.out.println(max + " " + min + " " + (double) sum / a.length);
    }
}
```

```text
45 7 23.5
```

### Reverse in place (two pointers)

```java
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        for (int l = 0, r = a.length - 1; l < r; l++, r--) {
            int t = a[l]; a[l] = a[r]; a[r] = t;
        }
        System.out.println(Arrays.toString(a));
    }
}
```

```text
[5, 4, 3, 2, 1]
```

### Second largest (one pass)

```java
public class Main {
    public static void main(String[] args) {
        int[] a = {10, 45, 45, 30, 7};
        int first = Integer.MIN_VALUE, second = Integer.MIN_VALUE;
        for (int x : a) {
            if (x > first) { second = first; first = x; }
            else if (x > second && x != first) second = x;
        }
        System.out.println(second);
    }
}
```

```text
30
```

More array patterns are in our [DSA arrays tutorial](https://skills101.in/learn/dsa/arrays-and-hashing).

## Common mistakes

- Looping with `i <= a.length` (one too far).
- Printing an array directly instead of `Arrays.toString`.
- Thinking `b = a` copies an array.

## Quick recap

- Fixed size, same type, index 0 … length − 1.
- `Arrays.sort`, `toString`, `fill`, `copyOf`, `binarySearch`.
- `b = a` shares; `a.clone()` copies.
- 2D arrays are arrays of arrays.


Practice quiz and free certificate: https://skills101.in/learn/java/java-arrays