开发者

How to initialize all integers in an array in Java?

Is there a predefined function in Java to set all integers in an array to a specified value?

Suppose the following array is given: byte start[][][] = new by开发者_开发问答te[MAXSUM][rows][N * 2]; Is there any nicer way than 3 loops to initialize it to some constant, other than 0?

Can Arrays.fill() be used to initialize values in an array with more than 1 dimension?


No, there is no built-in for that, but it shouldn't look very ugly to do it with nested for loops.

public static void multiDimensionalFill(byte[][][] start, byte value) {
    for(byte[][] firstdim : start)
        for(byte[] seconddim : firstdim)
            Arrays.fill(seconddim, value);
}


There is no nicer, built-in way to initialize a multi-dimensional array other than looping over the values.


Arrays.fill() works only for one dimension arrays

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜