Java #7 - Array (Part-2)

Java #7 - Array (Part-2)

1. Multi-dimensional array (2-Dimensional Array) 2. Array manipulations

Multi-dimensional array (2-Dimensional Array)

As we know that an array is a container of a single type, it can also contain another array which is also an object. A multi-dimensional array is an array of array.

For example, if we have to save students' marks studying in std. 1 to 5. Each std. has a respective student's marks which is an array. Instead of creating 5 variables that will hold marks of students we can create another array that will hold std. specific marks.

image.png

Array Declaration

Syntax - int[][] arrays = new int[rowSize][colSize];
Example - int twoD[][] = new int[4][5]; // It will declare twoD array with row 5 and column 5 for all rows

We can also declare an array with different column size as shown below:

int[][] grades = new int[5][];
grades[0] = new int[5];
grades[1] = new int[6];
grades[2] = new int[7];
grades[3] = new int[8];
grades[4] = new int[9];

Array Access

To access an element of the array we need to use row and column index

Syntax - arrays[rowIndex][colIndex]

Example 
arrays[0][1] = 10; // this will assign 10 to 1st row and 2nd column 
System.out.println(arrays[0][1]); // this will print value of element saved in 1st row and 2nd column

Array Initialization

We can also initialize arrays directly at the time of declaration.

int[][] array = {
                {0, 1, 2, 3},
                {4, 5, 6, 7}
};

Example

/*
This class will create two-dimensional array of different lengths and assign value and print array
 */
public class TwoDArray {

    public static void main(String[] args) {
        int[][] grades = new int[5][];
        grades[0] = new int[5];
        grades[1] = new int[6];
        grades[2] = new int[7];
        grades[3] = new int[8];
        grades[4] = new int[9];
        int k = 0;
        for(int i = 0; i < grades.length; i++){ //length is count of row
            for(int j = 0; j< grades[i].length; j++){ // length is count of column of each row
                k++;
                grades[i][j] = k;
            }
        }

        for (int[] std : grades){
            for (int marks : std){
                System.out.print(marks + " ");
            }
            System.out.println();
        }
    }
}

Memory representation of a two-dimensional array

image-2.png

Arrays Manipulation

Java provides the Arrays class which is part of package java.util (Collection framework) and contains various methods for manipulating arrays (such as sorting and searching) and to convert arrays into lists.

This class provides inbuilt methods to perform operations on the array, below are some examples -

  1. static int[] copyOf(int[] original, int newLength) - It returns new array with elements from original and newLength.
  2. static boolean equals​(int[] a, int[] a2) - It returns true if the two specified array values of ints are equal to one another.
  3. static void sort(int[] a) - Sorts the specified array into ascending numerical order.

Example

public class ArraysClassDemo {
    public static void main(String[] args) {
        int[] marks1 = {10,20,30};
        int[] marks2 = {10,20,30};
        int[] marks3 = {10,20,40};

        System.out.println(Arrays.equals(marks1,marks2)); // True
        System.out.println(Arrays.equals(marks1,marks3)); // False

        int[] input = {5,4,10,3,1,0,12,2};
        Arrays.sort(input);

        for(int val : input){
            System.out.print( val + " "); // 0 1 2 3 4 5 10 12 
        }
    }
}

Did you find this article valuable?

Support subodh's blog by becoming a sponsor. Any amount is appreciated!