二维数组排序程序添加功能

原程序为生成一定范围的随机数组并排序,在更新之前效果如下图:

 

 此时会在目录下输出四个结果文件:

更新了文件读入功能:

首先,将数据放入文件中,数字间用空格隔开:

 

然后使用程序进行读取:

可以看到文件中的数字被成功排序:

 

文件读取功能代码:

protected PointScanner(String inputFileName, Algorithm algo) throws FileNotFoundException, InputMismatchException
    {
        Scanner scnr = new Scanner(new File(inputFileName));
        ArrayList<Integer> apts = new ArrayList<Integer>();
        while(scnr.hasNextInt()) {
            apts.add(scnr.nextInt());
        }
        scnr.close();
        if (apts.size() % 2 != 0) throw new InputMismatchException();
        points = new Point[apts.size() / 2];
        int i;
        int x = 0;
        int y = 1;
        for(i = 0; i < points.length; i++) {
            points[i] = new Point(apts.get(x), apts.get(y));
            x = x + 2;
            y = x + 1;
        }
        sortingAlgorithm = algo;
    }

 

posted on 2021-03-14 17:00  Priao  阅读(37)  评论(0)    收藏  举报