Arrays.fill with multidimensional array in Java

后端 未结 13 1772
心在旅途
心在旅途 2020-11-27 18:49

How can I fill a multidimensional array in Java without using a loop? I\'ve tried:

double[][] arr = new double[20][4];
Arrays.fill(arr, 0);

相关标签:
13条回答
  • 2020-11-27 19:34

    how can I fill a multidimensional array in Java without using a loop?

    Multidimensional arrays are just arrays of arrays and fill(...) doesn't check the type of the array and the value you pass in (this responsibility is upon the developer).

    Thus you can't fill a multidimensional array reasonably well without using a loop.

    Be aware of the fact that, unlike languages like C or C++, Java arrays are objects and in multidimensional arrays all but the last level contain references to other Array objects. I'm not 100% sure about this, but most likely they are distributed in memory, thus you can't just fill a contiguous block without a loop, like C/C++ would allow you to do.

    0 讨论(0)
提交回复
热议问题