我有这个二维数组(比如 double[10][10]),其中包含一些 1.0 和 10.0,其余的都在 0.0 秒内。我试图遍历这个数组以找到 1.0(起点),从那里随机“移动”它(使用 random.nextInt(4))向上、向下、向左或向右直到它到达 10.0。我创建了一个 emptyArray 来跟踪它通过每个点移动了多少次(或者至少我认为我做到了)。编译时没有任何结果,但是当我尝试将其显示到框架中时没有得到任何结果。知道我哪里出错或失踪了吗?
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,1.0,1.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,10.0,10.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
{0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0}
二维数组的样本。
double[][] getPath(double[][] dataIn) {
double[][] emptyArray = new double[dataIn.length][dataIn[0].length];
double[][] drunkLoc = new double[dataIn.length][dataIn[0].length];
for (int i = 0; i < dataIn.length; i++) {
for (int j = 0; j < dataIn[i].length; j++) {
if (dataIn[i][j] == 1.0) {
double drunkHome = 10.0;
drunkLoc[i][j] = dataIn[i][j];
do {
int dir = getDirection();
switch(dir) {
case 0:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i-1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 1:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j-1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 2:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i+1][j];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
case 3:
if ((i > 0) && (j > 0)) {
drunkLoc[i][j] = drunkLoc[i][j+1];
double value = emptyArray[i][j];
emptyArray[i][j] = value + 1;
emptyArray[i][j] = (255<<24) | (255<<16) | (255<<8) | 255;
} else {
break;
}
break;
default:
}
} while (drunkLoc[i][j] != drunkHome);
}
}
}
return emptyArray;
}
如果您需要更多说明,请告诉我。只有我的第二个帖子,所以还在学习我的提问技巧。提前致谢。