为什么这会超出范围?

时间:2018-06-26 21:38:28

标签: java arraylist

一段时间以来,我一直在尝试解决这一部分的问题,似乎无法理解为什么这会不断抛出异常。故障排除似乎表明我不应该超越界限,但是产生的结果似乎不一致。例如,这是测试的结果。我真的不明白像this这样的事情会如何发生。这种在ArrayList中设置对象邻居的方法显然有什么问题吗?我只是想知道这里是否存在任何明显的逻辑错误,否则,我将不得不自己进行更多测试。谢谢您的帮助。

    public void setNeighbors(ArrayList<ArrayList<Objects>> arrayList) //sets the neighboring tiles for each tile
{
    //arrayList is an multi dimensional array of Objects with a variable size. It is created in another method
    for(int i = 0; i < arrayList.size(); i++)
    {
        for(int x = 0; x < arrayList.get(i).size(); x++)
        {
            // 1 2 3 
            // 4 @ 6
            // 7 8 9
            if((i>0&&x>0) && x<arrayList.get(i).size()-1 && i<arrayList.size()-1)
            {   //goes 1 3 7 9
                System.out.println("Max X:"+(arrayList.get(i).size()-1));
                System.out.println("Max Y:"+(arrayList.size()-1));
                System.out.println("X:"+x);
                System.out.println("Y:"+i);
                // A Floor is a type of Object and addNeighbor adds a Floor object to an ArrayList of Floor Objects within a Floor
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x-1));
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x+1));
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x-1));
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x+1));
            }
            if (x>0 && x<arrayList.get(i).size()-1) 
            { // 4 6
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i).get(x-1));
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i).get(x+1));
            }
            if (i>0 && i<arrayList.size()-1) 
            { // 2 8
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x));
                ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x));
            }

        }
    }
}

4 个答案:

答案 0 :(得分:1)

在您的if-statement中,您拥有:

if((i>0&&x>0) && x<arrayList.get(i).size()-1 && i<arrayList.size()-1)

因此,请检查以确保x在arrayList的ith范围内。但是,您更改了i。因此,虽然x可能在arrayList(i)的范围内,但可能不在arrayList(i + 1)的范围内

答案 1 :(得分:1)

由于代码似乎在x + 1的值上爆炸,因此任一行 ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x+1));

或行

((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x+1));

有错。无论哪种情况,都表明各个ArrayList的长度不是恒定的。

答案 2 :(得分:0)

ix已经达到其最大有效值时,您的代码仍可以尝试将其加1并将结果用于索引。

答案 3 :(得分:0)

原因是关于x + 1和i + 1的以下几行

 ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x+1));

您的if子句必须检查x和i变量的size()-2而不是-1。

不知道自己到底在做什么,应该可以。认为该算法无法以正确的方式工作,您必须检查

for(int x = 0; x < arrayList.get(i).size(); x++)
    {
        // 1 2 3 
        // 4 @ 6
        // 7 8 9
        if((i>0&&x>0) && x<arrayList.get(i).size()-2 && i<arrayList.size()-2)
        {   //goes 1 3 7 9
            System.out.println("Max X:"+(arrayList.get(i).size()-1));
            System.out.println("Max Y:"+(arrayList.size()-1));
            System.out.println("X:"+x);
            System.out.println("Y:"+i);
            // A Floor is a type of Object and addNeighbor adds a Floor object to an ArrayList of Floor Objects within a Floor
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x-1));
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x+1));
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x-1));
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x+1));
        }
        if (x>0 && x<arrayList.get(i).size()-2) 
        { // 4 6
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i).get(x-1));
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i).get(x+1));
        }
        if (i>0 && i<arrayList.size()-2) 
        { // 2 8
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i-1).get(x));
            ((Floor)arrayList.get(i).get(x)).addNeighbor((Floor)arrayList.get(i+1).get(x));
        }

    }
}