调用我的方法特定次数

huangapple 未分类评论49阅读模式
英文:

Calling my method a specific amount of times

问题

在我的一个测试案例中,它检查了我调用mergeSort方法的次数,当大于或等于三次时,测试通过。但是在我的代码中,我使用相同的对象调用该方法三次,但我的测试案例显示我没有调用它。

我的代码:

import java.util.ArrayList;

public class SortTester {

    public static void main(String[] args) {
        int[] testArray;
        long startTime, endTime;
        int arraySize = 50000;

        // 随机数组
        int[] random = makeRandomArray(arraySize);
        // 创建对象
        Sorter obj = new Sorter();

        // 记录开始时间
        startTime = System.currentTimeMillis();
        
        // 使用Merge Sort静态方法对数组进行排序
        obj.mergeSort(random, arraySize);
        
        // 记录结束时间
        endTime = System.currentTimeMillis();

        // 打印结果
        System.out.println("随机数组: " + (endTime - startTime));
        
        // 几乎有序数组
        int[] sorted = makeAlmostSortedArray(arraySize);
        // 与上面相同的步骤
        startTime = System.currentTimeMillis();
        obj.mergeSort(sorted, arraySize);
        endTime = System.currentTimeMillis();
        System.out.println("几乎有序数组: " + (endTime - startTime));
        
         // 逆序数组
        int[] reverse = makeReverseArray(arraySize);
        // 与上面相同的步骤
        startTime = System.currentTimeMillis();
        obj.mergeSort(sorted, arraySize);
        endTime = System.currentTimeMillis();
        System.out.println("逆序数组: " + (endTime - startTime));
    }
}
英文:

In one of my test cases, it checks the number of times that I call the mergeSort method and it passes when greater than or equal three. In my code, I call the that method three times with the same object yet my test case says I called it zero times.

My code:

import java.util.ArrayList;

public class SortTester {

public static void main(String[] args) {
    int[] testArray;
    long startTime, endTime;
    int arraySize = 50000;

    // Random Array
    int[] random = makeRandomArray(arraySize);
    // Make array
    Sorter obj = new Sorter();

    // Record start time
    startTime = System.currentTimeMillis();
    
    // Sort array using Merge Sort static method
    obj.mergeSort(random, arraySize);
    
    // Record end time
    endTime = System.currentTimeMillis();

    // Print out results
    System.out.println("Random array: " + (endTime - startTime));
    
    // Almost Sorted Array
    int[] sorted = makeAlmostSortedArray(arraySize);
    // Same steps as above
    startTime = System.currentTimeMillis();
    obj.mergeSort(sorted, arraySize);
    endTime = System.currentTimeMillis();
    System.out.println("Almost Sorted Array: " + (endTime - startTime));
    
    
     // Reverse Array
    int[] reverse = makeReverseArray(arraySize);
    // Same steps as above
    startTime = System.currentTimeMillis();
    obj.mergeSort(sorted, arraySize);
    endTime = System.currentTimeMillis();
    System.out.println("Reverse Array: " + (endTime - startTime));
    
}

huangapple
  • 本文由 发表于 2020年5月30日 06:21:33
  • 转载请务必保留本文链接:https://java.coder-hub.com/62095415.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定