K折算法中的数组越界

问题描述

我在运行项目时遇到问题,错误是数组超出范围,您可以帮我吗? 这是我的方法

public double hitungAkurasi(TrainRecord[] trainRecords,TestRecord[] testRecords,int K) {
    if (K <= 0) {
        System.out.println("K should be larger than 0!");
        return -1;
    }

    //normalisasi
    TrainRecord[] trainingSet = trainRecords;
    TestRecord[] testingSet = testRecords;

    //System.out.println("Test" + testRecords);

    trainingSet = (TrainRecord[]) normalisasi(trainingSet);
    testingSet = (TestRecord[]) normalisasi(testingSet);

    Record[] join = trainingSet;


    //test those TestRecords one by one
    int numOfTestingRecord = testingSet.length;
    int numOfTrainingRecord = trainingSet.length;
    int sumNumOfRecord = numOfTestingRecord + numOfTrainingRecord;

    //train record
    for (int i = 0; i < (sumNumOfRecord) / K; i++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet,testingSet[i],K);
        int classLabel = classify(neighbors);
        testingSet[i].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }
    for (int j = ((numOfTrainingRecord + 1) * sumNumOfRecord) / K; j < sumNumOfRecord; j++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet,testingSet[j],K);
        int classLabel = classify(neighbors);
        testingSet[j].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }

    //test record
    for (int t = (numOfTestingRecord * sumNumOfRecord) / K; t < ((numOfTestingRecord + 1) * sumNumOfRecord) / K; t++){
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet,testingSet[t],K);
        int classLabel = classify(neighbors);
        testingSet[t].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }

    int correctPrediction = 0;
    for (int l = 0; l < numOfTestingRecord; L++) {

        if (testingSet[l].predictedLabel == testingSet[l].classLabel) {
            correctPrediction++;
        }
    }

    return ((double) correctPrediction / numOfTestingRecord) * 100;
}

当我尝试禁用测试记录以成功使程序成功循环时,但是当我启用该循环时,这里出现了错误数组

TrainRecord[] neighbors = findKNearestNeighbors(trainingSet,K);

你能帮我解决这个问题吗?

解决方法

如果它在“测试记录”循环中抛出“数组索引超出范围”,则表示索引 t 在某个时候的取值大于您数组的大小。正在访问该循环。您可以检查修改循环以打印这些值和/或检查t是否超出范围:

for (int t = (numOfTestingRecord * sumNumOfRecord) / K; t < ((numOfTestingRecord + 1) * sumNumOfRecord) / K; t++){
        System.out.println("Array size: "+testingSet.length);
        System.out.println("Current index: "+t);   
        if (t > testingSet.length) {
            System.out.println("If you see this message you are in trouble");    
        }    
        TrainRecord[] neighbors = findKNearestNeighbors(trainingSet,testingSet[t],K);
        int classLabel = classify(neighbors);
        testingSet[t].predictedLabel = classLabel; //assign the predicted label to TestRecord
    }

您可能需要更改 t 的初始化或结束循环的条件。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...