Java设计二维数组
我有一个二维数组。是否有必要固定大小以使每行具有相同的列数?
data = {{ 2, 6, 3, 3, 1 },
{ 4, 6, 3, 7, 5 },
{ 8, 3, 0, 0, 0},
{ 13, 12, 0, 0, 0 },
{ 5, 1, 3, 9, 5, 0}}
I have a 2 dimensional array. Is it necessary to fix the size so that every row has the same number of columns?
data = {{ 2, 6, 3, 3, 1 },
{ 4, 6, 3, 7, 5 },
{ 8, 3, 0, 0, 0},
{ 13, 12, 0, 0, 0 },
{ 5, 1, 3, 9, 5, 0}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Java 中的数组数组不一定是“矩形”。只需去掉哨兵 (-1) 和它们后面的“空”数据,并使用子数组的 .length 即可。
Arrays of arrays in Java do not have to be "rectangular". Just get rid of the sentinel (-1) and the "empty" data after them and use the .length of the sub-array.
您可以忽略所有被忽略的值; java 2d 数组只是数组的数组。没有理由要求它们都具有相同的长度。
You can just omit all the ignored values; a java 2d array is just an array of arrays. There's no reason why they have to all be the same length.
不,没有必要固定 java 二维数组中每行的列大小。
作为“TofuBeer”答案的扩展,您可以动态分配二维数组,如下所示。下面的示例显示了三角矩阵:
No, it is not necessary to fix the size for the columns for each row in java's 2D array.
In extension to the answer of "TofuBeer" you can allocate the 2D array dynamically as below. the below sample shows the trangular matrix: