Create two dimensional array java

Create two dimensional array java

Below example will show you how to create two dimensional array in java. In this example we will create array with 8 rows and 8 column where first bucket denotes rows and second bucket denotes column. Please have example below:

package com.javahonk;

public class TwoDimensionalArray {

    public static void main(String[] args) {

        //First bucket is number of rows
        // Second bucket is number of columns
        //Below we have created 8 row and 8 column 2d array
        int[][] twoDimesionalArray = new int[8][8];

        for (int i = 0; i < twoDimesionalArray.length; i++) {
            for (int j = 0; j < 8; j++) {
                twoDimesionalArray[i][j] = j;
            }

        }

        for (int i = 0; i < twoDimesionalArray.length; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(twoDimesionalArray[i][j] + " ");

            }
            System.out.println(" ");
        }

    }

}

 

Output:
Create two dimensional array java

Leave a Reply

Your email address will not be published. Required fields are marked *