Java I';我重用排序数组,但我想使用新生成的数组

Java I';我重用排序数组,但我想使用新生成的数组,java,arrays,sorting,Java,Arrays,Sorting,在这段代码中,我一次又一次地重用排序数组,这就是为什么第二次的输出与第一次相比非常低。有人能帮忙吗 import java.util.Random; import java.util.Scanner; public class UseSorts { public static int[] makeArray(int x) { int[] a = new int[x]; for (int i = 0; i < x; i++) { Random r = new R

在这段代码中,我一次又一次地重用排序数组,这就是为什么第二次的输出与第一次相比非常低。有人能帮忙吗

import java.util.Random;
import java.util.Scanner;

public class UseSorts {

public static int[] makeArray(int x) {

    int[] a = new int[x];
    for (int i = 0; i < x; i++) {
    Random r = new Random(1234);
    a[i] = r.nextInt();
    }
    return a;
}

public static void InsertionSort(int[] x) {

    long start = System.nanoTime();
    Sorts.InsertionSort(x);
    long finish = System.nanoTime();
    long total = finish - start;
    System.out.println("elapsed time is: " + total);


} 
public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    System.out.print("Enter the number of elements: ");
    int number = in.nextInt();
    for (int i = 0; i < 10; i++) {

        int[] arr = makeArray(number);
        InsertionSort(arr);

    }

} 

}

除第一次运行外,所有其他运行都是一致的。您可能不得不考虑Eclipse开销或您使用的工具的开销来考虑第一次运行时间。我不确定确切的原因是什么,但是,我可以告诉你,你没有重复使用内存,所有其他类变量看起来都很好。如果忽略第一次运行,则所有其他运行都非常一致

你想要什么?在调用排序方法之前创建新数组?或者每次传递一个
clone()
(显然是在排序之前)?@Gabor-他是如何重用数组的?他在循环中分配内存。我不明白你的意思。你没有重用数组。Do int[]arr=makeArray(数字);System.out.println(arr.toString());运行此命令,您将看到不同的引用。如果重用数组元素,则在loop@FarhanSyed对不起,我错过了。我看到一个递归调用没有中断逻辑。这不应该引发stackoverflow异常吗?
Enter the number of elements: 100
elapsed time is: 1930646
elapsed time is: 7926
elapsed time is: 7692
elapsed time is: 7590
elapsed time is: 7555
elapsed time is: 7649
elapsed time is: 7560
elapsed time is: 7592
elapsed time is: 7685
elapsed time is: 12281