Beginner⏱️ 8 min📘 Topic 7 of 22

📋 Java Arrays — 1D, 2D, Initialization and Common Operations

Master Java arrays — declare, initialize, iterate, multi-dimensional arrays, Arrays utility methods (sort, fill, copyOf) and array vs ArrayList with examples and interview Q&A.

An array is a fixed-size, indexed container of one type. Arrays are objects in Java and live on the heap.

📜 Declaration & initialization

int[] a = new int[5];          // 5 zeros
int[] b = {1, 2, 3, 4};         // literal
String[] names = new String[3]; // 3 nulls

🔢 Access & length

0-indexed. a.length is a field (no parentheses). Out-of-bounds access throws ArrayIndexOutOfBoundsException.

🟦 Multi-dimensional & jagged

int[][] grid = new int[3][4];
int[][] jagged = { {1}, {1,2}, {1,2,3} };

🛠️ The Arrays utility class

  • Arrays.sort(a) — sort in place
  • Arrays.fill(a, 0) — set all elements
  • Arrays.copyOf(a, newLen) — resize copy
  • Arrays.toString(a) — printable form
  • Arrays.equals(a, b) — content compare

⚖️ Array vs ArrayList

Arrays are fixed-size and fast. ArrayList (covered in the Collections Framework topic) grows dynamically and is what you'll usually reach for. Need to convert? Arrays.asList(arr) or List.of(...).

💻 Code Examples

Sort and print

int[] nums = {5, 2, 8, 1};
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
Output:
[1, 2, 5, 8]

2D array traversal

int[][] grid = {{1,2},{3,4}};
for (int[] row : grid)
  for (int v : row)
    System.out.print(v + " ");
Output:
1 2 3 4

Resize with copyOf

int[] a = {1, 2, 3};
int[] bigger = Arrays.copyOf(a, 5);
System.out.println(Arrays.toString(bigger));
Output:
[1, 2, 3, 0, 0]

⚠️ Common Mistakes

  • Using a.length() with parentheses — array length is a field: a.length.
  • Accessing index a.length — valid indices are 0 to a.length-1; throws ArrayIndexOutOfBoundsException.
  • Comparing arrays with == — compares references; use Arrays.equals() for content.
  • Printing an array directly — System.out.println(arr) prints a hashcode; use Arrays.toString().

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.How do you find the length of an array vs a String vs an ArrayList?Beginner
Array: arr.length (field). String: str.length() (method). ArrayList: list.size() (method). A classic source of beginner bugs.
Q2.What happens if you access an array out of bounds?Beginner
Java throws ArrayIndexOutOfBoundsException at runtime — unlike C/C++, Java checks bounds for safety.
Q3.What's the difference between an array and an ArrayList?Intermediate
Array: fixed size, can hold primitives, faster, no built-in methods. ArrayList: dynamic size, objects only (autoboxes primitives), rich API (add/remove/contains), backed by an array internally.
Q4.What is a jagged array?Intermediate
A multi-dimensional array where each sub-array can have a different length, e.g., int[][] where row 0 has 2 elements and row 1 has 5. Java arrays of arrays are jagged by nature.

🧠 Quick Summary

  • Arrays are fixed-size, 0-indexed objects on the heap.
  • arr.length is a field; bounds are checked (exception on overrun).
  • Arrays class: sort, fill, copyOf, toString, equals.
  • Java supports jagged (ragged) multi-dimensional arrays.
  • Use ArrayList when you need dynamic size.