JAVA - Array | Multi dimensional array | Jagged Array


Array is a collection of similar type of elements that, all elements are stored in same memory location. We can store the fixed number of elements in the array( i mean the mentioned size of elements). 

There are two types of array 

One dimensional array 

Multi dimensional array 

To check the size of array 

Syntax : arrayName.length 

Declaring Array 

One Dimensional Array : dataType variable[ ] = new dataType[size]; 

Example : 

         int[ ] a = new int[5]; 
        int [ ]a = new int[5]; 
        int a[ ] = new int[5]; 
        int a[ ] = {1,2,3,4,5,6}; //Declaration & initialization in a single line 

Two dimensional array : dataType variable[ ][ ] = new dataType[row-size][column-size]; 

Example : 

int[ ][ ] a = new int[3][3]; 
int [ ]a[ ] = new int[3][3]; 
int a[ ][ ] = new int[3][3]; 
int [ ][ ]a = new int[3][3]; 
int a[ ][ ] = {{1,2,3},{4,5,6}};

Jagged Array 

Array in which number of columns different in each row called jagged array. 

int a[ ][ ] = new int[4][ ]; 

 a[0] = new int[5]; 
 a[1] = new int[2]; 
 a[2] = new int[1]; 
 a[3] = new int[4]; 

a[0]th row contains 5-columns 
a[1]th row contains 2-columns 
a[2]th row contains 1-columns 
a[3]th row contains 4-columns 

Note : - 

        To check the size of array or to get the size of array 
        To check the size of individual row of array 

 public class Program
        public static void main(String[] args) throws IOException 
            Scanner s = new Scanner(System.in);
            int a[][] = new int[3][];
            a[0]=new int[4];
            a[1]=new int[1];
            a[2]=new int[2];
            for(int i=0;i<a.length;i++)
                    System.out.println("Enter "+a[i].length+" elements");
                    for(int j=0;j<a[i].length;j++)
            System.out.println("Entered elements are");
            for(int i=0;i<a.length;i++)
                    for(int j=0;j<a[i].length;j++)
                            System.out.print(a[i][j]+" ");

Arrays is a class in java it is present in java.util package. The Arrays class contain lot of inbuilt 
methods which are going to help us to do the operations, like sort , search , copy one array to other 
and etc... and these methods are static methods. You can accesses them like Arrays.methodname
and pass the required fields has the arguments. The methods in this class throw NullPointerException 
if the specified array reference is null.

public static voidsort(int[] a)

        Sorts the specified array into ascending numerical order.

public static int binarySearch(int[] a , int key)

        Searches the specified array of ints for the specified value using the binary search algorithm. The array must be sorted (as by the sort(int[]) method) prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.

public static booleanequals(int[] a,int[] a2)

        Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.

public static void fill(int[] a,int val)

        Assigns the specified int value to each element of the specified array of ints.


"multi dimensional array in java"

"jagged array in java"

"3d jagged array in java"

"multidimensional array"

"two dimensional array in java"

"jagged array in java w3schools"

"jagged array in javatpoint"

"two dimensional string array in java example"