【发布时间】:2014-08-28 09:35:16
【问题描述】:
我已经编写了一个函数来在两个数组之间插入步数,但是在插入完成之前不知道所需的步数。
这是我的功能:
int[][] interpolate(int[] source, int[] goal){
int[] current = new int[source.length];
ArrayList<int[]> steps = new ArrayList<int[]>();
while(/* condition */){
// Change value of current
steps.add(current);
}
int[][] stepsArr = steps.toArray(new int[0][0]);
return stepsArr;
}
我尝试使用 ArrayList 在生成状态时存储它们,但发现 ArrayList 只存储指针,因此最终的 ArrayList 包含指向同一对象的多个指针(current 的最终值)。
有没有办法动态生成 int[] 实例以分步存储,或者生成二维整数数组?
【问题讨论】: