简体   繁体   English

Java 数组索引越界错误

[英]Java Array Index out of Bounds Error

I'm working on a brute force approach to the traveling salesman problem.我正在研究旅行商问题的蛮力方法。 I have a certain line that produces the ArrayIndexOutOfBounds exception, however all the arrays used there have more than enough space.我有一行会产生ArrayIndexOutOfBounds异常,但是那里使用的所有数组都有足够的空间。 The particular line of code:特定的代码行:

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

This is where I initialize testCity :这是我初始化testCity

int[][] testCity = new int[2][CITIES+10];

cities : cities

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

And, finally, cityList :最后, cityList

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

This is the entire error message:这是整个错误消息:

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)

And here is the code:这是代码:

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;
}
}

Why is this happenening?为什么会发生这种情况? What can I do to fix it?我能做些什么来修复它?

It is giving out of bound exception : 4它给出了越界异常:4
when you are initializing cityList ie cityList = arr.toArray();当你初始化 cityList ie cityList = arr.toArray(); your array cityList[] = {1,2,3,4} , ie of size 4 from 0 to 3.您的数组 cityList[] = {1,2,3,4} ,即从 0 到 3 的大小为 4。
And you are running a for loop ie你正在运行一个 for 循环,即

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

from a=0 to CITIES , so as the moment arrive when a=4, it gives out of bound error.从 a=0 到 CITIES ,所以当 a=4 时刻到来时,它会给出越界错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM