Main.java:88:错误:'。class'预期

时间:2014-02-13 11:27:38

标签: java

我在网站上发现这个代码可以正常工作5分但是我试了15分,但它给出了一个错误。

Compilation error   time: 0.07 memory: 380224 signal:0
Main.java:88: error: '.class' expected
int[] currSolution = new int[]{0, 3,1, 4,2, 7,6,8,5,10,14,11,13,12,0};  //initial solution
      ^
1 error

您可以查看here以及here

这是我把它放在neatbeans IDE7.4中的漏洞代码

它的工作正常5s,但不是15分     公共课TabuSearch {

    public static int[] getBestNeighbour(TabuList tabuList,
            TSPEnvironment tspEnviromnet,
            int[] initSolution) {


        int[] bestSol = new int[initSolution.length]; //this is the best Solution So Far
        System.arraycopy(initSolution, 0, bestSol, 0, bestSol.length);
        int bestCost = tspEnviromnet.getObjectiveFunctionValue(initSolution);
        int city1 = 0;
        int city2 = 0;
        boolean firstNeighbor = true;

        for (int i = 1; i < bestSol.length - 1; i++) {
            for (int j = 2; j < bestSol.length - 1; j++) {
                if (i == j) {
                    continue;
                }

                int[] newBestSol = new int[bestSol.length]; //this is the best Solution So Far
                System.arraycopy(bestSol, 0, newBestSol, 0, newBestSol.length);

                newBestSol = swapOperator(i, j, initSolution); //Try swapping cities i and j
                // , maybe we get a bettersolution
                int newBestCost = tspEnviromnet.getObjectiveFunctionValue(newBestSol);



                if ((newBestCost > bestCost || firstNeighbor) && tabuList.tabuList[i][j] == 0) { //if better move found, store it
                    firstNeighbor = false;
                    city1 = i;
                    city2 = j;
                    System.arraycopy(newBestSol, 0, bestSol, 0, newBestSol.length);
                    bestCost = newBestCost;
                }


            }
        }

        if (city1 != 0) {
            tabuList.decrementTabu();
            tabuList.tabuMove(city1, city2);
        }
        return bestSol;


    }

    //swaps two cities
    public static int[] swapOperator(int city1, int city2, int[] solution) {
        int temp = solution[city1];
        solution[city1] = solution[city2];
        solution[city2] = temp;
        return solution;
    }

    public static void main(String[] args) {

        TSPEnvironment tspEnvironment = new TSPEnvironment();

        tspEnvironment.distances = //Distance matrix, 5x5, used to represent distances
                new int[][] {{0, 55, 75, 115, 170, 148, 145, 159, 98, 101, 95, 50, 58, 20},
                            {55, 0, 43, 115, 165, 155, 170, 198, 142, 138, 115, 75, 105, 75},
                            {75, 43, 0, 80, 125, 120, 142, 181, 135, 123, 91, 64, 106, 95},
                            {115 , 115, 80, 0, 56, 41, 72, 124, 102, 80, 40, 66, 100, 124},
                            {170, 165, 125, 56, 0, 41, 85, 145, 120, 88, 121, 151, 178 },
                            {148, 155, 120, 41, 41, 0, 45, 105, 105, 80, 55, 96, 120, 153}, 
                            {145, 170, 142, 72, 85, 45, 0, 60, 75, 53, 53, 98, 103, 145},
                            {159, 198, 181, 124, 145, 105, 60, 0, 63, 60, 91, 123, 104, 150},
                            {98, 142, 135, 102, 120, 105, 75, 63, 0, 25, 60,72, 40, 88}, 
                            {101, 138, 123, 80, 120, 80, 53, 60, 25, 0, 40, 65, 50, 95},
                            {95, 115, 91, 40, 88, 55, 53, 91, 60, 40, 0, 45, 64, 98},
                            {50, 75, 64, 66, 121, 96, 98, 123, 72, 65, 45, 0, 46, 58},
                            {58, 105, 106, 100, 151, 120, 103, 104, 40, 50, 64, 46, 0, 47},
                            {20, 75, 95, 124, 178, 153, 145, 150, 88, 95, 98, 58, 47, 0},


    };
        //Between cities. 0,1 represents distance between cities 0 and 1, and so on.

        int[] currSolution = new int[]{0, 3,1, 4,2, 7,6,8,5,10,14,11,13,12,0};   //initial solution
        //city numbers start from 0
        // the first and last cities' positions do not change

        int numberOfIterations = 100;
        int tabuLength = 10;
        TabuList tabuList = new TabuList(tabuLength);

        int[] bestSol = new int[currSolution.length]; //this is the best Solution So Far
        System.arraycopy(currSolution, 0, bestSol, 0, bestSol.length);
        int bestCost = tspEnvironment.getObjectiveFunctionValue(bestSol);

        for (int i = 0; i < numberOfIterations; i++) { // perform iterations here

            currSolution = TabuSearch.getBestNeighbour(tabuList, tspEnvironment, currSolution);
            //printSolution(currSolution);
            int currCost = tspEnvironment.getObjectiveFunctionValue(currSolution);

            //System.out.println("Current best cost = " + tspEnvironment.getObjectiveFunctionValue(currSolution));

            if (currCost < bestCost) {
                System.arraycopy(currSolution, 0, bestSol, 0, bestSol.length);
                bestCost = currCost;
            }
        }

        System.out.println("Search done! \nBest Solution cost found = " + bestCost + "\nBest Solution :");

        printSolution(bestSol);



    }

    public static void printSolution(int[] solution) {
        for (int i = 0; i < solution.length; i++) {
            System.out.print(solution[i] + " ");
        }
        System.out.println();
    }
}

错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 14
    at TSPEnvironment.getObjectiveFunctionValue(TSPEnvironment.java:22)
    at TabuSearch.main(TabuSearch.java:94)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

enter image description here

1 个答案:

答案 0 :(得分:4)

前一行结束

, 0},

它应以

结尾
, 0}};

查看您的第一个链接

{5, 8, 1, 2, 0}};

我建议您尝试使用像IntelliJ CE这样的IDE进行开发,因为它可以帮助您更轻松地格式化代码和错误。