Java 如何检查二维数组是否超出范围?

Java 如何检查二维数组是否超出范围?,java,arrays,Java,Arrays,如果形状在给定的row/col位置有填充块,则方法isFilledAt()返回true;如果块为空,则方法false。如果该位置超出范围,则使用信息性消息引发一个FitItException。我正在运行一个嵌套的循环来获取位置,并且很难确定位置是否超出边界。中小企业能帮忙吗?提前谢谢 public class CreateShape { private int height; private int width; private char dc; private

如果形状在给定的
row/col
位置有填充块,则方法
isFilledAt()
返回
true
;如果块为空,则方法
false
。如果该位置超出范围,则使用信息性消息引发一个
FitItException
。我正在运行一个嵌套的循环来获取位置,并且很难确定位置是否超出边界。中小企业能帮忙吗?提前谢谢

public class CreateShape {

    private int height;
    private int width;
    private char dc;
    private Rotation initialPos;


    public CreateShape(int height, int width, char dc)
    {
        this.height = height;
        this.width = width;
        this.dc = dc;
        initialPos = Rotation.CW0;
    }
public boolean isFilledAt(int row, int col) 
    {
        char[][] tempArray = new char[height][width];
        for(int i = 0; i < tempArray.length; i++)
            for(int j = 0; j < tempArray[i].length; j++)
            {
                if(row > tempArray.length || row < 0)
                    throw new FitItException("Out of Bounds!");

                if(tempArray[row][col] == dc)
                    return true;
            }

        return false;
    }
公共类CreateShape{
私人内部高度;
私有整数宽度;
私人区议会;
私人轮换;
公共CreateShape(整数高度、整数宽度、字符dc)
{
高度=高度;
这个。宽度=宽度;
this.dc=dc;
initialPos=旋转.CW0;
}
公共布尔值isFilledAt(整数行,整数列)
{
字符[][]临时数组=新字符[高度][宽度];
for(int i=0;itempArray.length | |行<0)
抛出新的FitException(“出界!”);
if(临时数组[行][列]==dc)
返回true;
}
返回false;
}

您需要检查
是否小于零,或者
是否大于或等于
高度
,或者
是否大于或等于
宽度
。注意,您只需验证一次,就可以将检查移到循环之外:

public boolean isFilledAt(int row, int col)  {
    if (row < 0 || row >= height || col < 0 || col >= width) {
        throw new FitItException("Out of Bounds!");
    }
    char[][] tempArray = new char[height][width];
    for (int i = 0; i < tempArray.length; i++) {
        for (int j = 0; j < tempArray[i].length; j++) {
            if (tempArray[row][col] == dc) {
                return true;
            }
        }
    }
    return false;
}
public boolean isFilledAt(int行,int列){
如果(行<0 | |行>=高度| |列<0 | |列>=宽度){
抛出新的FitException(“出界!”);
}
字符[][]临时数组=新字符[高度][宽度];
for(int i=0;i

但是请注意,
isFilledAt()
可能无法按预期工作。由于每次调用该方法时都会重新创建
tempArray
,因此条件
tempArray[row][col]==dc
可能永远不会计算为
true

您应该按如下所示更改isFilledAt方法

public boolean isFilledAt(int row, int col) 
{
    char[][] tempArray = new char[height][width];
    // Calls the method that fills tempArray datas

    if ( (row >= height || row <0)  || (col >= width || col < 0)) {
         throw new FitItException("Out of Bounds!");
    }

    for(int i = 0; i < height; i++)
        for(int j = 0; j < width; j++)
        {
                if(tempArray[row][col] == dc)
                return true;
        }


   }
   return false;
}
public boolean isFilledAt(int行,int列)
{
字符[][]临时数组=新字符[高度][宽度];
//调用填充临时数组数据的方法
如果((行>=高度| |行=宽度| |列<0)){
抛出新的FitException(“出界!”);
}
对于(int i=0;i

您将返回值放在错误的位置:如果查找的值不在第一行内,它将系统性地返回false。相反,在迭代每一行后返回false

方法
isFilledAt
应该做什么?当然
tempArray
应该是一个实例变量,并且该方法应该测试它的一个条目吗?目前
tempArray
是一个局部变量,因此您对它所做的更改将丢失。谢谢@AndersonVieira@Nikolay很乐意帮忙!虽然这应该可以解决“越界”问题。关于
tempArray
,您的代码中似乎仍然存在一些错误。你应该考虑一下PbAbcDFP的评论。你是说在TEMPARY中有什么不对劲?环路有问题吗@AndersonVieira@Nikolay我在答案的末尾添加了一个小解释。@Nikolay您可能希望在
CreateShape
中有一个
char[][]
实例字段,并相应地填充它。