开发者

How do I create a 2d int array with different lengths in scala?

How do I do the scala equivalent of this java code

int[开发者_Python百科][] vals = new int[4][];
for (int i=0; i < vals.length; i++) {
  vals[i] = new int[1 + 2*i];
}

The Array.ofDim method takes two parameters


Like this:

Array.tabulate(4)(i => Array.ofDim[Int](1 + 2 * i))

It will be much slower, however. If this code is in a critical path, you should do a while loop to make it much like in Java.


One way to do this would be:

Array.tabulate(4)(i => new Array[Int](1 + 2 * i))
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜