0

我有这个二维数组(比如 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;
}

如果您需要更多说明,请告诉我。只有我的第二个帖子,所以还在学习我的提问技巧。提前致谢。

4

1 回答 1

1

好的,供您学习,这里有一些示例代码,它将为您提供您正在寻找的答案(在 penDimension x 的“棋盘”上从 (xpos,ypos) 到 (destX,destY) 的移动次数penDimension 大小:

        int penDimension = 10;
        int destX = 2;
        int destY = 2;
        int xpos = 5;
        int ypos = 5;

        // Add this to keep track of no moves through each square
        int[][] moveCounts = new int[penDimension][penDimension];

        Random r = new SecureRandom();
        long noMoves = 0;
        while (xpos != destX || ypos != destY) {
            switch (r.nextInt(4)) {
            case 0 : xpos++; break;
            case 1 : xpos--; break;
            case 2 : ypos++; break;
            case 3 : ypos--; break;
            }
            if (xpos < 0) xpos = 0;
            if (ypos < 0) ypos = 0;
            if (xpos > penDimension) xpos = penDimension;
            if (ypos > penDimension) ypos = penDimension;
            noMoves++;

            // Add this to keep track of no moves through each square
            moveCounts[ypos][xpos]++;

        }
        System.out.println("Number of moves: " + noMoves);

除了执行 ++ 或 -- 然后检查边界,您还可以编写(在现实生活中可能会编写),例如:

xpos = Math.max(0, xpos - 1);

我只是像上面那样写它,因为我认为它更容易理解。

除了编写“new SecureRandom()”之外,您还可以编写“new Random()”,这可能是您所学的。但是 SecureRandom 是一个质量更高(但速度更慢)的随机数生成器。一般来说,在编写反复生成大量随机数的“模拟”时,最好避免使用标准的 Random 类并使用更高质量的生成器。

于 2012-07-02T10:45:38.133 回答