ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
Java Tutorial• 6 min read
Arrays & Multi-Dimensional Matrices
Fixed-size memory allocation, 2D matrices, and java.util.Arrays utilities
Arrays in Java are fixed-length objects storing elements of identical type in contiguous memory.
Working with Arrays and Matrices
Declaration & Allocation: int[] numbers = new int[5]; initializes with default zeros.
Literal initialization: String[] tracks = {"Java", "Python", "SQL"};.
Arrays utility class: Arrays.sort(arr), Arrays.binarySearch(arr, val), Arrays.toString(arr).
2D Arrays (Matrix): int[][] matrix = new int[3][3];.
2D Matrix Processing in Java
import java.util.Arrays;
public class MatrixDemo {
public static void main(String[] args) {
int[] scores = {95, 72, 88, 61, 100};
Arrays.sort(scores);
System.out.println("Sorted: " + Arrays.toString(scores));
// 2x2 Identity Matrix
int[][] identity = {
{1, 0},
{0, 1}
};
System.out.println("Center element: " + identity[0][0]);
}
}ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which property returns the total number of elements in a Java array?