2-D/Multi-dimensional array

Image_20.3.jpg

Screenshot (1263).png

double[][] arr = new double[2][3];
double[][] arr = new double[][]{ { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };
double[][] arr = { { 1.1, 2.2, 3.3 }, { 4.4, 5.5, 6.6 } };

Ragged array

Screenshot (1264).png

Screenshot (1265).png

int[][] arr = new int[4][];
arr[0] = new int[] { 11 };
arr[1] = new int[] { 22, 33 };
arr[2] = new int[] { 44, 55, 66 };
arr[3] = new int[] { 77, 88, 99, 110 };
for(int i=0; i<arr.length; i++) {
    for(int j=0; j<arr[i].length; j++) {
        System.out.print(arr[i][j] + ", ");
    }
}

Variable Arity Method

Screenshot (1269).png

Methods with a variable number of arguments. These arguments are represented by ... and internally collected into an array.

public static int sum(int... arr) {
    int total = 0;
    for(int num: arr)
        total = total + num;
    return total;
}

public static void main(String[] args) {
    int result1 = sum(10, 20);
    System.out.println("Result: " + result1);
    int result2 = sum(11, 22, 33);
    System.out.println("Result: " + result2);
}

Method Overloading

Screenshot (1266).png

Screenshot (1267).png