磁盘优化 - Java中的SCAN算法

时间:2017-12-12 15:42:25

标签: java

我目前正在尝试创建一个名为arrangeBySCAN的方法,该方法接收previous,current和sequence数组的参数。此方法的目的是在磁盘优化中根据SCAN格式排列.properties文件的输入。
然而,我收到了一个错误:

  

IllegalArgumentException

在以下代码行中:

int scanArray1[] = Arrays.copyOfRange(tmp, 0, currentIndex); //line 17

我不是一位经验丰富的程序员,我们将不胜感激。

以下是以下代码。

    private int[] arrangeBySCAN(int previous, int current, int sequence[]){
        int direction = previous - current;
        int seqLength = sequence.length;
        int tmp[] = new int[seqLength];
        // If direction is less than 0, it is increasing
        // If direction is greater than 0, it is decreasing
        if (direction < 0) {
            Arrays.sort(tmp);
        }
        else if (direction > 0){
            Arrays.sort(tmp);
            Collections.reverse(Arrays.asList(tmp));
        }
        // Find the index position of the current cylinder after sorting
        int currentIndex = Arrays.asList(tmp).indexOf(current);
        // Split the sequence into two parts, taking the index of the current cylinder as reference
        int scanArray1[] = Arrays.copyOfRange(tmp, 0, currentIndex);
        int scanArray2[] = Arrays.copyOfRange(tmp, currentIndex, tmp.length);
        // Reverse first sequence
        Collections.reverse(Arrays.asList(scanArray1));

        // Append the first sequence onto the second sequence
        int scanArray[] = new int[scanArray1.length + scanArray2.length];
        System.arraycopy(scanArray1, 0, scanArray, 0, scanArray1.length);
        System.arraycopy(scanArray2, 0, scanArray, currentIndex ,scanArray2.length);
        return scanArray;
    }

1 个答案:

答案 0 :(得分:1)

IllegalArgumentException归因于currentIndex为-1。您最有可能的例外情况是有一条消息“0&gt; -1”。 currentIndex为-1,因为您从未在tmp数组中添加任何内容,因此indexOf返回-1,除非current恰好为0。