Chapter 8: Arrays

1. What is an Array? (The Big Idea)

An array is a collection of elements of the same type stored in contiguous memory locations. You can think of it as a row of numbered lockers in a school corridor:

  • Each locker can hold one item (of the same type)
  • Each locker has a number (index) starting from 0
  • You can access any locker quickly if you know its number

Real-life analogy: A row of 10 tiffin boxes on your dining table — all boxes are for food (same type), and you can say “give me the 3rd box” (index 2).

Key points:

  • Fixed size (once created, you can’t change how many elements it holds — unlike ArrayList)
  • All elements must be of the same data type
  • Index starts at 0 and goes to length-1
  • Arrays are reference types (they live on the heap)

2. Single-Dimensional Arrays (1D Arrays — The Most Common)

A. Declaring an Array

Java

B. Creating an Array (Allocating Memory)

Java

C. Declare + Create + Initialize in One Line

Java

Complete Example Program — Student Marks

Java

Output:

text

3. Multi-Dimensional Arrays (2D Arrays — Like Tables/Matrices)

Think of a 2D array as a spreadsheet or chessboard — rows and columns.

Syntax:

Java

Direct initialization:

Java

Accessing elements:

Java

Complete Example — 2D Array (Student Marks in 3 Subjects)

Java

Output:

text

4. Array Operations (Copy, Sort, Search)

A. Copying Arrays

Wrong way (shallow copy — same reference!):

Java

Correct ways:

  1. Manual copy (loop)
Java
  1. Arrays.copyOf() (recommended)
Java
  1. System.arraycopy() (fastest for large arrays)
Java

B. Sorting Arrays

Use Arrays.sort() — very efficient!

Java

For descending order (trick):

Java

C. Searching in Arrays

  1. Linear Search (simple, but slow for large arrays)
Java
  1. Binary Search (super fast — but array must be sorted!)
Java

Quick Recap Table (Your Cheat Sheet)

Feature Syntax / Method Notes / Example
1D Array Declaration int[] arr; or int arr[]; Preferred: int[]
Create & Initialize int[] arr = new int[5]; or {1,2,3} Default: 0 for int, null for objects
Length arr.length No () — it’s a field!
Access arr[0], arr[arr.length-1] Index 0 to length-1
2D Array int[][] matrix = new int[3][4]; matrix[row][col]
Copy (safe) Arrays.copyOf(arr, arr.length) Or System.arraycopy()
Sort Arrays.sort(arr) Ascending by default
Binary Search Arrays.binarySearch(arr, value) Array must be sorted! Returns index or <0

Common Mistakes & Fixes

Mistake Problem Fix
int[] arr = new int[5] then arr[5] ArrayIndexOutOfBoundsException Max index = length-1
int[] a = b; then modify a Both arrays change (same reference) Use Arrays.copyOf()
Arrays.sort() on unsorted for binarySearch Wrong results Sort first!
String[] names = new String[3]; then print without init Prints null null null Initialize: names[0] = “Webliance”;
Forgetting import java.util.Arrays; Cannot find symbol for sort/copyOf Import it!

Homework for You (Practice to Master!)

  1. Basic: Create an array of 7 days of the week. Print them using for-each loop.
  2. Medium: Take 5 numbers from user → store in array → find min, max, sum, average.
  3. Advanced: Create 2D array 4×4 → fill with random numbers 1–100 → print matrix → find sum of each row & column.
  4. Fun: Create array of your favorite 5 movies → sort them alphabetically → print.
  5. Challenge: Write linear search and binary search methods — compare how many comparisons each makes for a large array.

You’re doing amazing! Arrays are the foundation for so many things — lists, matrices, data processing, games, etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *