Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使此代码更短、更清晰_Java_Sorting - Fatal编程技术网

Java 如何使此代码更短、更清晰

Java 如何使此代码更短、更清晰,java,sorting,Java,Sorting,我把这些代码放在一起对数字进行排序。它的工作,我想更短,更干净,如果可能的话。我用两台扫描仪扫描同一个文件。我不太清楚如何使用方法来接受输入文件 import java.io.*; public class test { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(new File("/Users/WalterCueva/Desktop/4.t

我把这些代码放在一起对数字进行排序。它的工作,我想更短,更干净,如果可能的话。我用两台扫描仪扫描同一个文件。我不太清楚如何使用方法来接受输入文件

import java.io.*;


public class test {
    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(new File("/Users/WalterCueva/Desktop/4.txt")); //provide file name from outside
        int counter = 0; //keep track of how many iargs[0]ntegers in the file

        while (scan.hasNextInt()) {
            counter++;
            scan.nextInt();
        }
        Scanner scan2 = new Scanner(new File("/Users/WalterCueva/Desktop/4.txt"));
        int[] numbers = new int[counter];
        for (int i = 0; i < numbers.length; i++) {
           numbers[i] = scan2.nextInt(); //fill the array with the integers
        }
        for (int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
            ; //fill the array with the integers
        }

        new test().sort(numbers);
        System.out.println(Arrays.toString(numbers));
    }

    public void sort(int[] data) {

        for (int i = 0; i < data.length - 1; i++) {
            for (int j = 0; j < data.length - 1 - i; j++) {
                // do the swap if required
                if (data[j] > data[j+1]) {
                    int tmp = data[j+1];
                    data[j+1] = data[j];
                    data[j] = tmp;
                }
            }
        }
    }
}
import java.io.*;
公开课考试{
公共静态void main(字符串[]args)引发异常{
Scanner scan=new Scanner(新文件(“/Users/WalterCueva/Desktop/4.txt”);//从外部提供文件名
int counter=0;//跟踪文件中有多少IARG[0]NTEGER
while(scan.hasNextInt()){
计数器++;
scan.nextInt();
}
Scanner scan2=新扫描仪(新文件(“/Users/WalterCueva/Desktop/4.txt”);
int[]数字=新的int[计数器];
for(int i=0;i数据[j+1]){
int tmp=数据[j+1];
数据[j+1]=数据[j];
数据[j]=tmp;
}
}
}
}
}

替换这部分代码:

int[] numbers = new int[counter];
for (int i = 0; i < numbers.length; i++) {
    numbers[i] = scan2.nextInt(); //fill the array with the integers
}
for (int i = 0; i < numbers.length; i++) {
    System.out.println(numbers[i]);
    ; //fill the array with the integers
}
new test().sort(numbers);
int[]数字=新的int[计数器];
for(int i=0;i
为此:

int[] numbers = new int[counter];
for (int i = 0; i < numbers.length; i++) {
    numbers[i] = scan2.nextInt(); //fill the array with the integers
    System.out.println(numbers[i]);
}
Arrays.sort(numbers);
int[]数字=新的int[计数器];
for(int i=0;i

并删除公共void排序(int[]data)
函数。

我相信这是最短的时间了

首先,我们使用一个
列表
,它随着添加更多数据而动态增长。这可以防止您需要在数据上迭代两次

其次,我们使用内置函数
Collections.sort()
,它使用Tim sort。Tim sort将能够以最坏情况下的时间复杂度对数据进行排序,即
N log(N)
(与bubblesort实现相比,它是

导入java.io.File;
导入java.util.*;
公开课考试{
公共静态void main(字符串[]args)引发异常{
尝试(扫描仪扫描=新扫描仪(新文件(“text.txt”)){
列表=新的ArrayList();
while(scan.hasNextInt())
添加(scan.nextInt());
集合。排序(列表);
系统输出打印项次(列表);
}
}
}

使用纯Java 8的东西可以更轻松地完成这项工作。看看这个:

public class TestClass {

    private static final String FILE_LOCATION = "C:\\tmp\\file.txt";

    public static void main(String[] args) {
        List<Integer> lines = new ArrayList<>();
        try (Stream<String> stream = Files.lines(Paths.get(FILE_LOCATION))) {
            lines = stream.map(Integer::parseInt).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        lines.sort(Comparator.naturalOrder());
        lines.forEach(System.out::println);
    }

}
公共类TestClass{
私有静态最终字符串文件\u LOCATION=“C:\\tmp\\FILE.txt”;
公共静态void main(字符串[]args){
列表行=新的ArrayList();
try(Stream=Files.line(path.get(文件位置))){
lines=stream.map(Integer::parseInt).collect(Collectors.toList());
}捕获(IOE异常){
e、 printStackTrace();
}
line.sort(Comparator.naturalOrder());
lines.forEach(System.out::println);
}
}
导入java.io.*;
导入java.util.*;
公开课考试{
公共静态void main(字符串[]args)引发异常{
File File=新文件(“/Users/WalterCueva/Desktop/4.txt”);
如果(!file.exists()){
System.out.println(“文件不存在”);
系统出口(0);
}
扫描仪输入=新扫描仪(文件);
ArrayList编号=新的ArrayList();
while(input.hasNext()){
numbers.add(input.nextInt());//用整数填充数组
}
input.close();
用于(双e:数字){
系统输出打印ln(e);
}
java.util.Collections.sort(数字);
系统输出打印项次(数字);
}}

如果代码有效,如果这不是一项排序任务,则此问题可能更适合,您可以简单地使用
数组。排序(数据)
此问题属于堆栈交换网络中的另一个站点:codereview.stackexchange.com
public class TestClass {

    private static final String FILE_LOCATION = "C:\\tmp\\file.txt";

    public static void main(String[] args) {
        List<Integer> lines = new ArrayList<>();
        try (Stream<String> stream = Files.lines(Paths.get(FILE_LOCATION))) {
            lines = stream.map(Integer::parseInt).collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        lines.sort(Comparator.naturalOrder());
        lines.forEach(System.out::println);
    }

}
 import java.io.*;
    import java.util.*;
    public class test {
     public static void main(String[] args) throws Exception {

         File file = new File ("/Users/WalterCueva/Desktop/4.txt");
          if (!file.exists()) {
                System.out.println("The file does not exist ");
                System.exit(0);
            }

         Scanner input = new Scanner(file);

        ArrayList <Integer>numbers = new ArrayList<>();


  while(input.hasNext()) {
            numbers.add( input.nextInt()); //fill the array with the integers
        }

       input.close();

       for (double e: numbers) {
         System.out.println(e);
       }

    java.util.Collections.sort(numbers);
    System.out.println(numbers);

    }}