繁体   English   中英

如何在Java中动态创建多维数组?

[英]how to create multi dimensional array dynamic in java?

我的数组是静态的,现在我想使用for循环使其动态

int[][] args = new int[][]{{6815, 11524},{6845, 11567},{6815, 11524}};

我想使用for循环添加值,我该怎么做

我尝试过但没有完成,例如

for(int k=0;k<5;k++){
  // here i can add both both value {6815, 11524} as int.
}

有可能做到吗?

我相信您想做的是这样的:

// Create a 2D array of length of 5
int[][] args = new int[5][];
// Iterate from 0 to 4
for(int k=0;k<5;k++){
    // Affect the value of the array for the index k
    args[k] = new int[]{6815, 11524};
}

注意:如果您不知道数组的大小,请考虑使用列表 该代码将是这样的:

List<int[]> args = new ArrayList<>();
...
args.add(new int[]{6815, 11524});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM