用Java复制和打印数组

时间:2015-02-19 18:22:12

标签: java arrays sorting

此程序的目标是读取包含1到100000之间整数的文件,然后执行插入排序和快速排序。我正在读取的第一个文件按升序包含1到100000的整数。我目前遇到的问题是在尝试打印包含整数的数组时;它会在完成之前重复打印。例如,在运行程序时,它会在停止之前多次打印数组,也会阻止我进一步向上滚动。此外,当滚动到控制台的侧面时。底部数组搞砸了。我正在使用eclipse作为我的IDE:

滚动到一边之前: enter image description here

滚动到一边 enter image description here

我很难过为什么会这样。任何建议都会有所帮助。

public class Program {

public static void insertionSort(int[] a){
    for(int i = 1; i < a.length; i++){
        int temp = a[i];
        int j;
        for (j = i -1; j >= 0 && temp < a[j]; j--){
            a[j+1] = a[j];
        }
        a[j+1] = temp;
    }
}

public static int n = 100000;
public static void main(String[] args) throws FileNotFoundException{
    Scanner in = new Scanner(System.in);
    System.out.println("Please enter the name of the file to open");
    String fileName = in.nextLine();
    File file = new File(fileName);
    Scanner reader = new Scanner(file);
    int[] original = new int[n];
    int count = 0;
    while(reader.hasNextInt()){
        original[count] = reader.nextInt();
        count++;
    }
    in.close();
    reader.close();
    int duplicate[] = new int[original.length];
    for(int i = 0; i < original.length; i++){
        duplicate[i] = original[i];
    }
    long startTime = System.currentTimeMillis();
    insertionSort(duplicate);
    long estimatedTime = System.currentTimeMillis() - startTime;
    System.out.println("Function took " + estimatedTime +"ms");
    System.out.println(Arrays.toString(duplicate));
}

}

0 个答案:

没有答案