Java数组索引超出界限错误

时间:2015-08-30 15:42:02

标签: java arrays

我正在研究旅行商问题的蛮力方法。我有一条产生ArrayIndexOutOfBounds异常的行,但是那里使用的所有数组都有足够的空间。特定的代码行:

testCity[0][a] = cities[0][(int) cityList[a]];

这是我初始化testCity

的地方
int[][] testCity = new int[2][CITIES+10];

cities

public static int[][] cities = new int[2][CITIES+10];

最后,cityList

Object[] cityList = new Integer[CITIES+10];

这是整个错误消息:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at BruteF.permute(BruteF.java:39)
at BruteF.permute(BruteF.java:30)
at BruteF.permute(BruteF.java:30)
at BruteF.permute(BruteF.java:30)
at BruteF.main(BruteF.java:11)

以下是代码:

public class BruteF {
public static final int CITIES = 5;
public static int[][] cities = new int[2][CITIES+10];
public static int[][] bestCity = new int[2][CITIES+10];
public static double bestDistance = 1000;
public static int[][] testCity = new int[2][CITIES+10];
public static Object[] cityList = new Integer[CITIES+10];
public static void main(String[] args)
{
    permute(java.util.Arrays.asList(1,2,3,4), 0);
    for (int i = 0;i < CITIES;i++)
    {
        System.out.println(bestCity[0][i] + "," + bestCity[1][i]);
    }
}
static void permute(java.util.List<Integer> arr, int k){
    cities[0][0] = 1;
    cities[1][0] = 1;
    cities[0][1] = 2;
    cities[1][1] = 5;
    cities[0][2] = 3;
    cities[1][2] = 2;
    cities[0][3] = 4;
    cities[1][3] = 3;
    int originalX = cities[0][0];
    int originalY = cities[1][0];
    for(int i = k; i < arr.size(); i++){
        java.util.Collections.swap(arr, i, k);
        permute(arr, k+1);
        java.util.Collections.swap(arr, k, i);
    }
    if (k == arr.size() -1){
        for (int i = 0;i < CITIES;i++)
        {
            cityList = arr.toArray();
            for (int a = 0;a < CITIES;a++)
            {
                testCity[0][a] = cities[0][(int) cityList[a]];
            }
            if (distance(testCity,CITIES,originalX, originalY) < bestDistance)
            {
                bestCity = testCity;
                bestDistance = distance(testCity,CITIES, originalX, originalY);
            }
        }
    }
}
static double distance (int[][] cities, int CITIES, int originalX, int originalY)
{
    int[][] taken = new int[2][CITIES+1];
    int takenCounter = 0;
    double distance = 0;
    cities[0][CITIES] = cities[0][0];
    cities[1][CITIES] = cities[1][0];
    for (int i = 0;i <= CITIES;i++)
    {
        for (int z = 0;z <= CITIES;z++)
        {
            if (cities[0][i] == taken[0][z] && cities[1][i] == taken[1][z])
            {
                return CITIES*1000; //possible error here
            }
            else {
            taken[0][takenCounter] = cities[0][i];
            taken[1][takenCounter] = cities[1][i];
            }
        }
        if (cities[0][0] != originalX && cities[1][0] != originalY)
        {
        return CITIES*1000;                             //POSSIBLE BUG HERE
        }
        distance = distance + Math.sqrt(Math.pow(cities[0][i+1]-cities[0][i],2) + Math.pow(cities[1][i+1]-cities[1][i],2));
    }
    return distance;
}
}

为什么会这样?我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:0)

它发出了异常的例外:4
当你初始化cityList,即cityList = arr.toArray();你的数组cityList [] = {1,2,3,4},即大小为4,从0到3 你正在运行for循环,即

for (int a = 0;a < CITIES;a++)

从a = 0到CITIES,当a = 4时到达时刻,它会发出超出范围的错误。