Rommel Rico: Random ramblings

How to do Jagged Arrays in Java?

This is a brief tutorial on how to do jagged arrays in Java.

Most of the time, when we thing of arrays, we think of  M*N rectangular arrays, where M represents the rows and N represents the columns. These type of arrays are easy to create in Java:


public class arrayExample1 {
    public static void main(String[] args) {
        //Array is declared and initialized.
        int[][] array = new int[10][5];
        //Fill the array
        for (int i = 0; i<10; i++) {
            for (int j = 0; j<5; j++) {
                array[i][j] = j;
            }
        }
        //Test the array
        for (int i = 0; i<10; i++) {
            for (int j = 0; j<5; j++) {
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}

The output would be something like this:


0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4
0 1 2 3 4

But Rommel Rico can also create jagged arrays in Java! That is, we can create two-dimensional arrays that have M rows but each row has X columns. 'Whaaat!?', you say. Look:

public class arrayExample2 {
    public static void main(String[] args) {
        //Declare, Initialize, and fill the array
        int[][] array = {{0,1,2,3,4},
                         {0,1,2},
                         {0,1,2,3},
                         {0,1},
                         {0},
                         {0},
                         {0,1,2,3,4,5},
                         {0},
                         {0,1,2,3},
                         {0,1,2,3}};

        //Test the array
        for (int i = 0; i

The output would be something like this:

0 1 2 3 4
0 1 2
0 1 2 3
0 1
0
0
0 1 2 3 4 5
0
0 1 2 3
0 1 2 3
  • Could you possibly do a post on how jagged arrays can be used and when they would be preferred over normal arrays? Thanks!

  • Awesome :D I’ll check it out here shortly!

  • Awful!!! I’ll never check it out….:P
    sala nalaik….

  • yeah, you make me open my eclipse to try this, it´s true; there is no agreement to Initialize or print an array, good observation.
    sorry, but with my Eclipse open i start to make some, experiment and find this :

    public class ClaseA {

    public ClaseA() {
    throw new RuntimeException();
    }

    public static void main(String arguments[]) {

    int b[][] = new int[3][3];

    for (int i = 0; i < b.length; i++) {
    for (int j = 0; j < b.length; j++) {
    System.out.print(b[i][j]);
    }
    System.out.println();
    }
    int a[][] = new int[3][];
    for (int i = 0; i < a.length; i++) {
    System.out.println(a[i]);
    }

    }
    }

    This is the Ouput:
    000
    000
    000
    null
    null
    null

    jeje, the second array has "no auto-Initialize"
    rare for me :)
    thanks for the info dude! see you at SC n_n

    peace!

  • hey! forget about the Constructor in ClaseA!! , it was other experiment :x just ignore it :x

  • sir tell me best java(basic) book or link

  • Could you tell me the best spring book or link….

  • Stunning essay, acquired the enjoyment of reading

  • It can be tough to write about this topic. I think you did an excellent job though! Thanks for this! 21766

  • As soon as I discovered this web site I went on reddit to share some with the enjoy with them. 972278

  • Well I definitely liked reading it. This information procured by you is very practical for correct planning.

  • Nice one for the right to discuss this, I think boldy a ton and consequently adoration reading through more this important idea. If ever possibility, since you discover understanding, you feelings upgrading your web page as of furthermore facts? This is very useful for my lifestyle.

  • How may be the new year going? I hope to read a lot more fascinating posts like last year 391384

  • More than and more than once again I like to take into consideration this troubles. As a matter of fact it wasn

  • Excellent post, I conceive site owners need to larn a whole lot from this site its really user friendly . 578654

You can follow any responses to this entry through the RSS 2.0 feed.