How to initialize 2D array with zero in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {

    public static void main(String[] args) {

        int[][] array2D = new int[4][5]; // for primitives - the default value is zero 
     
        for (int i = 0; i < 4; i++ ) {
            for (int j = 0; j < 5; j++ ) {
                System.out.print(array2D[i][j] + " ");
            }
            System.out.println();
        }
    }
}
 
/*

run:

0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 

*/

 



answered Oct 8, 2016 by avibootz
...