Abstract Data Structures
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.
int[][] matrix = new int[5][10]; // Declares a 5-row, 10-column 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| -----------------------------------------
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
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(); }
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 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(); }
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.
By default, Java arrays are indexed starting from 0. However, if needed, we can adapt logic to use 1-based indexing.
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
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.
Stacks are commonly used in:
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); } }
import java.util.Stack; public class StackExample { public static void main(String[] args) { Stackstack = 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); } }
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.
Queues are commonly used in:
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); } }
import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queuequeue = 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); } }