Java 旅行商蛮力最近邻算法,循环问题

Java 旅行商蛮力最近邻算法,循环问题,java,algorithm,traveling-salesman,Java,Algorithm,Traveling Salesman,在过去的一个小时左右,我一直在摆弄我的主要方法的循环,但毫无结果。问题来自于值数组和单位矩阵中的数组越界 假设所有其他方法等都有效 public static void main(String args[]) { double adjacency_matrix[][] = new double[80][80]; double[] values = new double[160]; String[]contents = new String[160]; FileI

在过去的一个小时左右,我一直在摆弄我的主要方法的循环,但毫无结果。问题来自于值数组和单位矩阵中的数组越界

假设所有其他方法等都有效

public static void main(String args[])
{
    double adjacency_matrix[][] = new double[80][80];
    double[] values = new double[160];
    String[]contents = new String[160];

    FileIO reader = new FileIO();
    contents = reader.load("C:\\Users\\Mark\\Documents\\Java Workspace\\CS211\\src\\TSP\\locations.txt");

    double temp1,temp2,temp3,temp4;
    int count = 0,count2 = 0;

    for(int i=0; i<values.length; i++)
    {
        values[i] = Double.parseDouble(contents[i].substring(0,contents[i].length()-1));
    }

    for(int i=0; i<=79;i++)
    {
        for(int j=0; j<=79; j++)
        {
            if(i == j)
            {
                adjacency_matrix[i][j] = 0.0;

            count2+=1;
            }

            else
            {
                temp1 = values[i+count];
            temp2 = values[i + count + 1];
            temp3 = values[j + count2];
            temp4 = values[j +count2 + 1];

            adjacency_matrix[i][j] = GPSDistanceHarversine.gpsDistance(temp1, temp2, temp3, temp4);

            count2+=1;
            }

            count+=1;
            count2 = 0;
        }
    }

    System.out.println("The cities are visited as follows:");

    TspMain tspNearestNeighbour = new TspMain();

    tspNearestNeighbour.tsp(adjacency_matrix);
}
publicstaticvoidmain(字符串参数[])
{
双邻接矩阵[][]=新的双[80][80];
double[]值=新的double[160];
字符串[]内容=新字符串[160];
FileIO读取器=新FileIO();
contents=reader.load(“C:\\Users\\Mark\\Documents\\Java Workspace\\CS211\\src\\TSP\\locations.txt”);
双节拍1,节拍2,节拍3,节拍4;
int count=0,count2=0;

对于(int i=0;离子)发生错误的行数是多少?根据嵌套循环范围(从78、79、80)的变化,我会在以下各种情况下得到错误:temp1=values[i+count];temp2=values[i+count+1];temp3=values[j+count2];temp4=values[j+count2+1];邻接矩阵[i][j]=GPSDistanceHarversine.gpsDistance(temp1,temp2,temp3,temp4);在我认为正确的当前循环设置中,它在第111行temp2=值[i+计数+1]处给出错误;使用有意义的变量名称在这种情况下,它们唯一的意义是在内容数组中拉出正确的槽。基本上,我的文件有160行,第1行是town1的纬度,第2行是town1的经度,依此类推。临时变量需要考虑它们在每次循环调用中需要进行的跳跃,以获得正确的值每个城镇的纬度和经度,然后haversine函数计算城镇x到y的距离。抱歉没有说得更清楚。