'How to Initialize 2d ArrayList with 0 in one line

How to Initialize all the element of 2d Array List in Java with 0 in single line of code without using for lop.

ArrayList of m rows and n columns.

How to initialise value of all the elements to 0 at the time of defining it.



Solution 1:[1]

Not sure what you meant by 2D ArrayList, but you returned type of List instead of ArrayList with help of below:

List<List<Integer>> a=Arrays.asList(Arrays.asList(0));

For normal arrays you can do something :

int[][] b=new int[1][2];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ashish Patil