Create and Initialize Two Dimensional Array in Java

Java Array Examples


This program demonstrates how to create and initialize two dimensional Array in Java.

Learn complete Arrays in Java at https://www.javaguides.net/2018/08/java-array-basics-guide.html

Create and Initialize Two Dimensional Array in Java

package net.javaguides.corejava.arrays;

/**
 * This class will show how to initialize two dimensional array
 * @author javaguides.net
 *
 */
public class TwoDimentionalArray {

    public static void main(String[] args) {

        // an array of int arrays of 2 row and 3 columns
        int[][] twoDimArray = new int[2][3];
        for (int i = 0; i < twoDimArray.length; i++) {
            for (int j = 0; j < twoDimArray[i].length; j++) {
                twoDimArray[i][j] = j;
                System.out.print(twoDimArray[i][j] + " ");
            }
            System.out.println("");
        }

        // an array of String arrays of 3 rows and 4 columns
        String[][] arrStr = new String[3][4];
        for (int i = 0; i < arrStr.length; i++) {
            for (int j = 0; j < arrStr[i].length; j++) {
                arrStr[i][j] = "Str" + j;
                System.out.print(arrStr[i][j] + " ");
            }
            System.out.println("");
        }

        // creating and initializing two dimensional int array with shortcut
        // syntax
        int[][] arrInt = {
            {
                1,
                2,
                3
            },
            {
                3,
                4,
                5
            }
        };
        for (int i = 0; i < arrInt.length; i++) {
            for (int j = 0; j < arrInt[i].length; j++) {
                System.out.print(arrInt[i][j] + " ");
            }
            System.out.println("");
        }
    }
}
Output:
0 1 2 
0 1 2 
Str0 Str1 Str2 Str3 
Str0 Str1 Str2 Str3 
Str0 Str1 Str2 Str3 
1 2 3 
3 4 5 

Reference


Comments