Ads Notes
Ads Notes
1. isEmpty() Method
Explanation
The isEmpty method checks whether an array has any elements.
Java Code
2. size() Method
Explanation
The size method returns the number of elements in the array.
Java Code
3. insert() Method
Explanation
The insert method adds an element at a specified index, shifting the rest.
Java Code
public void insert(int element, int index) {
if (size == arr.length) {
throw new RuntimeException("Array is full");
}
if (index < 0 || index > size) {
throw new RuntimeException("Invalid index");
}
for (int i = size; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = element;
size++;
}
4. del() Method
Explanation
The del method removes an element at a specified index and shifts the elements left.
Java Code
5. indexOf() Method
Explanation
The indexOf method finds the first occurrence of an element.
Java Code
6. get() Method
Explanation
The get method retrieves the element at a specified index.
Java Code
7. display() Method
Explanation
The display method prints all elements in the array.
Java Code
public void display() {
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
Exercises
1. Insert elements in an array following a specific
pattern
Explanation
Insert numbers following the sequence 1, 2, 2, 3, 4, 4, 5, etc., with minimal and maximal
movements.
Java Code
Multi-Dimensional Arrays
In Java, a multi-dimensional array is essentially an array of arrays. For example, a 2D array
represents data in a table-like structure with rows and columns. Each row is itself an array,
making it possible to access elements using two indices.
Explanation:
2D Arrays
Row Major Order
Elements are stored row by row. For a 2D array with m rows and n columns, the mapping
function is:
Explanation:
Explanation:
3D Arrays
For a 3D array with dimensions p x q x r , indices are defined as:
i (0 ≤ i < p)
j (0 ≤ j < q)
k (0 ≤ k < r)
Explanation:
Explanation:
Solution:
To reverse both the row and column orders, replace i with (m - 1 - i) and j with (n -
1 - j) . In standard row-major order, the index is:
normal_index = i * n + j
Complexity:
Matrices
A matrix is a rectangular array of numbers arranged in rows and columns. In Java, matrices
are represented using 2D arrays [cite: 87, 88]. Matrices are fundamental in various fields
such as computer graphics, machine learning, and scientific computation.
Matrix Methods
read()
Reads matrix elements from the user.
Complexity:
print()
Prints the matrix.
Complexity:
add(int[][] A, int[][] B)
Adds two matrices element-wise.
Complexity:
multiply(int[][] A, int[][] B)
Multiplies two matrices.
Complexity:
Diagonal Matrix
Only the diagonal elements are nonzero. We store them in a 1D array.
import java.util.Scanner;
public DiagonalMatrix(int n) {
this.n = n;
diag = new int[n];
}
import java.util.Scanner;
public LowerTriangularMatrix(int n) {
this.n = n;
elements = new int[n * (n + 1) / 2];
}
import java.util.Scanner;
public UpperTriangularMatrix(int n) {
this.n = n;
elements = new int[n * (n + 1) / 2];
}
Tridiagonal Matrix
A tridiagonal matrix has nonzero elements only on the main diagonal and the diagonals
immediately above and below it. We store these in three separate arrays.
import java.util.Scanner;
public TridiagonalMatrix(int n) {
this.n = n;
lower = new int[n - 1];
main = new int[n];
upper = new int[n - 1];
}
Conclusion
This document now includes:
Each mapping and operation includes an analysis of time (mostly O(1) for index calculations
and up to O(n³) for full matrix operations) and space complexities.