Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有办法更好地处理网格上的8个相邻值检查?_Java - Fatal编程技术网

Java 有没有办法更好地处理网格上的8个相邻值检查?

Java 有没有办法更好地处理网格上的8个相邻值检查?,java,Java,目前为止,我已经创建了一组工作代码,该代码采用下面的grid定义的网格值。当我进行此检查时,我不确定该值是否与多个越界问题一致。我并没有展示所有的代码,但为了详细说明这段代码,它被包装在一个for循环中,用于网格的整个大小,其中值E是基点。这个for循环使用x和y,因此假设e=网格中该点处的值。除此之外,我想知道是否有可能在不失去功能的情况下减少此代码的冗余并使其更简单 //assume e has been checked to be in bounds already but the ou

目前为止,我已经创建了一组工作代码,该代码采用下面的grid定义的网格值。当我进行此检查时,我不确定该值是否与多个越界问题一致。我并没有展示所有的代码,但为了详细说明这段代码,它被包装在一个for循环中,用于网格的整个大小,其中值E是基点。这个for循环使用x和y,因此假设e=网格中该点处的值。除此之外,我想知道是否有可能在不失去功能的情况下减少此代码的冗余并使其更简单

 //assume e has been checked to be in bounds already but the outside squares have not 
//(and that they need to have there data changed if out of bounds to = e and if not then they need to be changed to the value at that point in the grid)
    double a=0,b=0,c=0,d=0,f=0,g=0,h=0,i=0;
    boolean ay = false,by= false,cy= false,dy= false,fy= false,gy= false,hy= false,iy= false;
                            if (x==0) 
                            {a=e;d=e;g=e;
                             ay = true;dy = true;gy = true;}
                            if (y==0) 
                            {a=e;b=e;c=e;
                            ay = true;by = true;cy = true;}
                            if (x== grid.length-1)
                            {c= e;f=e;i=e;
                            cy = true;fy = true;iy = true;}                 
                            if (y== grid.length-1)
                            {g=e;h=e;i=e;
                            gy = true;hy = true;iy = true;}

                            if (ay== false)
                                a =grid(y-1, x-1);
                            if (by== false)
                                b= grid(y-1,x);
                            if (cy== false)
                                c =grid(y-1,x+1);
                            if (dy== false)
                                d =grid(y,x-1);
                            if (fy== false)
                                f =grid(y,x+1);
                            if (gy== false)
                                g =grid(y+1,x-1);
                            if (hy== false)
                                h= grid(y+1,x);
                            if (iy== false)
                                i =grid(y+1,x+1);
编辑:
此代码旨在从网格中提取数据,并将其用于坡度计算。如果给定点没有数据(或数据超出范围),则该数据将被指定为中心值。这部分代码的目标是确保在检查数组数据时没有空点错误。

使用数学方法来简化和澄清:

int minI = Math.max(x - 1, 0);
int maxI = Math.min(grid.length - 1, x + 1)
int minJ = Math.max(y - 1, 0);
int maxJ = Math.min(grid.length - 1, y + 1)

for (int i = minI; i <= maxI; i++) {
    for (int j = minJ; j <= maxJ; j++) {
        if (i != x && j != y) {
            // do your test in here
        }
    }
}
intmini=Math.max(x-1,0);
int maxI=Math.min(grid.length-1,x+1)
int minJ=Math.max(y-1,0);
int maxJ=Math.min(grid.length-1,y+1)

对于(int i=minI;我有一些问题让我畏缩不前,干脆结束它-对不起,也许有比我更勇敢的人会有力量提供帮助。如果你们需要澄清,我会更乐意更新我的帖子的细节或在这里。也许只是描述一下代码应该做什么。@OldCurmudgeon怎么做?我理解我确实理解这是一种非常可怕的编码形式,它应该被火烧掉。因此,当我开始思考时,为什么要检查值是否在数组的正确区域,然后检查它们是否在数组中。我会喜欢使用空值检查使其看起来更好,但不幸的是,双值的范围不包括空值,因此是布尔值。更新了更多信息。在阅读这篇文章时,这段代码将做的是确定一个数字范围,只要这些数字是在边界内的吗?我已经将其视为一个可行的解决方案,但发现在这个解决方案中,数据之外的所有值都被排除在外。我假设这更适合于数据数组,对吗?如果是这样,那么我还想假设嵌套在for语句中的if语句可以有一个else语句来包含范围之外的值?