【问题标题】:Java Two-Dimensional Array of Objects -- How to check if a certain index exists?Java二维对象数组——如何检查某个索引是否存在?
【发布时间】:2012-04-21 01:13:41
【问题描述】:

我尝试使用 if 语句查看索引是否返回 null,如果返回则跳过,但它仍然不断获取数组索引超出范围异常。

...

    if(tiles[x+a][y+b] == null){
        System.out.println("Would have been an error");
            } else if(tiles[x+a][y+b].getType() == type) {
                tiles[x+a][y+b].setTile(type);
            } else {
                System.out.println("Not found");
            }
...

主要是前两行是问题的内容。我试图让它通过数组,但跳过任何会使数组索引超出范围异常的内容。如果您需要更多背景信息,请告诉我。我尽量减少它。

【问题讨论】:

    标签: java arrays if-statement multidimensional-array null


    【解决方案1】:

    怎么样:

    if ( ( (x+a) >= tiles.length ) || ( (y+b) >= tiles[x+a].length ) )

    作为你的测试?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2017-03-24
      • 2013-03-21
      • 1970-01-01
      相关资源
      最近更新 更多