[英]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.