Programming & Scripting Tutorials

Java: Multidimensional Array Tables



Creating a Multidimensional array table is actually fairly difficult, and you might find it quite hard.
Here is the code, we will go through it after:


public class Main {
     public static void Display(int Array[][]){

        for(int row=0;row<Array.length;row++){

            for(int column=0;column<Array[row].length;column++){

                System.out.print(Array[row][column] + "\t");
            }
            System.out.println();
        }

    }

    public static void main(String[] args) {
        int ArrayOne[][]={{32, 43, 65, 2, 23}, {54, 39, 77, 83}};
        System.out.println("This is the array:");
        Display(ArrayOne);
    }

}



First I would like to explain this:

public static void Display(int Array[][]){


This is a simple function (like the class member functions) but its in the main class, it takes a multidimensional array as its parameters. We make it public and static so we can refer to it via our main function.


for(int row=0;row<Array.length;row++){


The above line of code simple starts a for loop, the loop continues to loop until the row number (represented by variable "row") is higher than the array length (the number of rows).


for(int column=0;column<Array[row].length;column++){


The above line is another for loop, this one is for the columns. It continues to loop while there is columns("column<Array[row].length" is a little trick to check the one we are on).


System.out.print(Array[row][column] + "\t");

This simply prints out the row and column specified. We use print instead of println as we want multiple values on one line.


System.out.println();

The above simple starts a new line.


This Java tutorial was written by


Back to Java

Advertisement: