by Dinesh Thakur Category: array

when we created arrays of arrays (i.e. two-dimensional array), the arrays in the array were of same length i.e. each row had the same number of elements. As each array in a multidimensional array is a separate array so it can have different number of elements. Such arrays are known as non-rectangular arrays or ragged arrays.

In order to create a two-dimensional ragged array, you have to first declare and instantiate the array by specifying only the number of elements in the first dimension and leaving the second dimension empty. For example: Consider the statement,

int [] [] table = new int [3] [];

This statement declares an array object table of type int [] [] which references an array of 3 elements, each of which holds a reference to a one-dimensional array. With this statement, although three one-dimensional arrays have been declared but the number of elements in each one dimensional array is still undefined. So you can define these arrays individually as follows,

tab1e[0] = new int[l];

table [1] = new int[2];

table [2] = new int[3];

These statements define three possible one-dimensional arrays that can be referenced through the elements of table array. The first element (table [0]) now references an array of one element of type int. The second element (table [1]) now references an array of two elements of type int. The third element (table [2]) now references an array of three elements of type int.

                       Non-Rectangular array table

Like you can initialize arrays of arrays, similarly, the ragged arrays can also be initialized. For example : The statement,

int table [] [] = { {l},{2,2},{3,3,3}};

creates an int-type two dimensional array table containing three array elements. The first element is reference to a one-dimensional array of length one, the second element is reference to one-dimensional array of length two and the third elements is a reference to one-dimensional array of length three.

                      Arrays Of Arrays with Varying Length in Java

You can access the elements of the ragged array just as before with the index pair but now the range of the index for each subarray is different.

Now let us consider a program to find the minimum and maximum in a ragged array

//program that find maximum and minimum in ragged array

public class MaxMin
         public static void main(String [] args)
           int[] [] table= { {15,10} ,{5,12,34},{3,32,17,44}};
           int i, j,max,min;
           max = min = table [0] [0];
                          if(table[i] [j]>max);
                            max = table[i] [j];
                          if(table[i] [j]<min)
                             min = table[i] [j];
                        System.out.println("Maximum Element is : "+max);
                        System.out.println("Minimum Element is  : "+min);

Variable length (Dynamic) Arrays in Java

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article