Two Dimensional Array

A two-dimensional array (or matrix) is an array where each element is itself an array. These are used to represent tables, grids, and mathematical matrices.

Declaration and Initialization

int[][] matrix = new int[5][10]; // Declares a 5-row, 10-column matrix
        

Graphical Representation of a Matrix

A 2D array can be visualized as a table with rows and columns:

    0   1   2   3   4   5   6   7   8   9
  -----------------------------------------
0 | 34 -53  14  91  87 -23  14  12  57 -6 |
1 |-87  48 -51  7   38  1   11  18  8   15|
2 | 21  31  30  1   24 -2  -7   23  64  5 |
3 |-8   5   8  -1  -2   22  28  25  22  25|
4 | 39  4   3   0   47  3   17  53  27  38|
  -----------------------------------------
        

Indexing in a 2D Array

Each element in a 2D array is accessed using two indices: one for the row and one for the column. Example:

matrix[2][4] = 10; // Assigns the value 10 to the element at row index 2 and column index 4
        

Traversing a 2D Array

Row-wise Traversal

for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        System.out.print(matrix[i][j] + " ");
    }
    System.out.println();
}
        

Column-wise Traversal

for (int j = 0; j < matrix[0].length; j++) {
    for (int i = 0; i < matrix.length; i++) {
        System.out.print(matrix[i][j] + " ");
    }
    System.out.println();
}
        

Reading and Printing a 2D Array

Reading a matrix:

Scanner scanner = new Scanner(System.in);
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        matrix[i][j] = scanner.nextInt();
    }
}
        

Printing a matrix:

for (int[] row : matrix) {
    for (int elem : row) {
        System.out.print(elem + " ");
    }
    System.out.println();
}
        

Memory Usage of a 2D Array

The memory occupied by a 2D array depends on:

Example:

int[][] largeMatrix = new int[1000][1000];
    

This matrix contains 1,000,000 elements, each taking 4 bytes, totaling approximately 4MB.

Base Indexing in 2D Arrays

By default, Java arrays are indexed starting from 0. However, if needed, we can adapt logic to use 1-based indexing.

Example: Accessing Specific Elements

System.out.println(matrix[0][0]); // First element
System.out.println(matrix[1][9]); // Last column of second row
System.out.println(matrix[4][3]); // Fifth row, fourth column
    

Common Mistakes in 2D Arrays

Applications of 2D Arrays

Stack

A stack is a linear abstract data structure where elements are added and removed from the same end, called the top of the stack. The stack follows the LIFO (Last In, First Out) principle.

Stack Operations

When to Use a Stack?

Stacks are commonly used in:

Stack Implementation in Java

Using an Array

class StackArray {
    private int maxSize;
    private int[] stackArray;
    private int top;

    public StackArray(int size) {
        this.maxSize = size;
        this.stackArray = new int[maxSize];
        this.top = -1;
    }

    public void push(int value) {
        if (top == maxSize - 1) {
            System.out.println("Stack is full");
            return;
        }
        stackArray[++top] = value;
    }

    public int pop() {
        if (top == -1) {
            System.out.println("Stack is empty");
            return -1;
        }
        return stackArray[top--];
    }

    public int peek() {
        return (top == -1) ? -1 : stackArray[top];
    }

    public boolean isEmpty() {
        return (top == -1);
    }
}
    

Using Java's Stack Class

import java.util.Stack;

public class StackExample {
    public static void main(String[] args) {
        Stack stack = new Stack<>();
        stack.push(10);
        stack.push(20);
        stack.push(30);

        System.out.println("Top element: " + stack.peek());
        stack.pop();
        System.out.println("Stack after pop: " + stack);
    }
}
    

Applications of Stacks

Queue

A queue is a linear abstract data structure where elements are added at one end (rear) and removed from the other end (front), following the FIFO (First In, First Out) principle.

Queue Operations

When to Use a Queue?

Queues are commonly used in:

Queue Implementation in Java

Using an Array

class QueueArray {
    private int maxSize;
    private int[] queueArray;
    private int front;
    private int rear;
    private int size;

    public QueueArray(int size) {
        this.maxSize = size;
        this.queueArray = new int[maxSize];
        this.front = 0;
        this.rear = -1;
        this.size = 0;
    }

    public void enqueue(int value) {
        if (size == maxSize) {
            System.out.println("Queue is full");
            return;
        }
        rear = (rear + 1) % maxSize;
        queueArray[rear] = value;
        size++;
    }

    public int dequeue() {
        if (size == 0) {
            System.out.println("Queue is empty");
            return -1;
        }
        int removed = queueArray[front];
        front = (front + 1) % maxSize;
        size--;
        return removed;
    }

    public int peek() {
        return (size == 0) ? -1 : queueArray[front];
    }

    public boolean isEmpty() {
        return (size == 0);
    }
}
    

Using Java's Queue Interface

import java.util.LinkedList;
import java.util.Queue;

public class QueueExample {
    public static void main(String[] args) {
        Queue queue = new LinkedList<>();
        queue.add(10);
        queue.add(20);
        queue.add(30);

        System.out.println("Front element: " + queue.peek());
        queue.remove();
        System.out.println("Queue after dequeue: " + queue);
    }
}
    

Applications of Queues