简体   繁体   English

读取文本文件(使用ArrayList进行分隔,排序,查找数字之间)

[英]Reading Text File (Separating, Sorting, Find the Between Numbers With ArrayList)

I'm really shy to say this problem. 我真的不好意思说这个问题。 Because i couldn't understand how to do it. 因为我不知道该怎么做。 I've a text file. 我有一个文本文件。 In this text file there is a 1 million numbers. 在此文本文件中,有一百万个数字。 These are between 0 to 999 and every line contains a number. 它们在0到999之间,并且每一行都包含一个数字。 I have to separate these numbers with (\\n), that means every line will contains one integer number. 我必须用(\\ n)分隔这些数字,这意味着每一行将包含一个整数。 Then i have to sort these. 然后,我必须对这些进行排序。 After sorting i will take 2 inputs from user. 排序后,我将接受用户的2个输入。 Then i have to find and write numbers to a new text file between these inputs in the first text file. 然后,我必须在第一个文本文件中的这些输入之间找到数字并将其写入新的文本文件中。

I know most people will say "we're not fool. You don't know anything, this site is not like this." 我知道大多数人会说“我们不是傻瓜。您一无所知,这个网站不是这样的。” That's right, but i have to do this. 是的,但是我必须这样做。 I just want to know how can i do this. 我只想知道我该怎么做。 Which methods or strategies do i need? 我需要哪些方法或策略? Because i've found similar things in here but i don't know are these true with my work. 因为我在这里找到了类似的东西,但我不知道这些对我的工作是否正确。

For reading the file I suggest you use this, will make your task quite simple. 为了阅读文件,我建议您使用它,这会使您的任务非常简单。
http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

For sorting, look at this class, it has a sort method. 对于排序,请查看此类,它具有一个sort方法。
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

For converting String values (which you read from the file) 用于转换字符串值(从文件中读取)
to int or Integer values use this class, it has parseInt method. 要int或Integer值使用此类,它具有parseInt方法。
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

Here's a simple example using Java 8: 这是一个使用Java 8的简单示例:

public static void main(final String[] args) throws IOException {
    final Pair input = readInput();
    final Path sourceFile = Paths.get("path", "to", "input", "file");
    final Path destFile = Paths.get("path", "to", "output", "file");
    final IntStream parsed = Files.lines(sourceFile).
            mapToInt(Integer::parseInt).
            filter(i -> i >= input.getLow() && i <= input.getHigh()).
            parallel().
            sorted();
    try (final PrintWriter outputFile = new PrintWriter(Files.newBufferedWriter(destFile, StandardCharsets.UTF_8))) {
        parsed.forEach(outputFile::println);
    }
}

private static Pair readInput() {
    final Console console = System.console();
    final String low = console.readLine("Please enter the start number (inclusive): ");
    final String high = console.readLine("Please enter the end number (inclusive): ");
    return new Pair(Integer.parseInt(low), Integer.parseInt(high));
}

private static final class Pair {
    private final int low;
    private final int high;

    private Pair(int low, int high) {
        this.low = low;
        this.high = high;
    }

    public int getHigh() {
        return high;
    }

    public int getLow() {
        return low;
    }
}

The readInput() method uses a Console to read the high and low values into a class Pair that holds the user input. readInput()方法使用Console将高值和低值读入保存用户输入的Pair类中。

The main method calls readInput() then reads lines from the input file, filters out numbers that are lower than low and higher than high and parallel sorts the resultant output. main方法调用readInput()然后从输入文件中读取行,过滤出低于low和高于high数字,并对输出结果进行并行排序。

Once we have the pipeline setup, we then call forEach on it and write the value to the output file. 设置好管道之后,就可以在其上调用forEach并将其值写入输出文件。

For sorting numbers you can use counting sort algorithm: you have numbers of limited range, from 0 to 999. So, you can count how many times each number occurred in input file: 对于数字排序,您可以使用计数排序算法:您可以使用范围从0到999的数字。因此,您可以计算输入文件中每个数字出现的次数:

int[] count = new int[1000];
//depending of scope, maybe you need to fill array with zeroes

...
int number = ...;//read number from file;
count[number]++;

Now, in order to output it as sorted, you just need two loops like this: 现在,为了按排序输出它,您只需要两个这样的循环:

for (int i = 0; i < 1000; ++i) {
    for (int j = 0; j < count[i]; ++j) {
        System.out.println(i);
    }
}

I wrote this just to make sure idea is clear, although your task doesn't require to output them all. 我写这篇文章只是为了确保思路清晰,尽管您的任务不需要全部输出。 Now, let's imagine you read these two numbers; 现在,让我们假设您阅读了这两个数字;

int start, finish = 0; //value read from input

We gonna modify the loops above like this: 我们将像这样修改上面的循环:

int counter = 0;
for (int i = 0; i < 1000; ++i) {
    for (int j = 0; j < count[i]; ++j) {
        if ((counter >= start) && (counter < finish)) {//or maybe <=, depending on what exactly do you need
            System.out.println(i);
        }
        counter++;
        if (counter > finish) { //we finished, further iterations are waste of CPU time
            break(2);
        }  
    }
}

This method is not optimal, but easy to understand and implement. 此方法不是最佳方法,但易于理解和实施。 For more fast solutions, you may want to get rid of inner loop, replacing it with logic to add count[i] to counter at once and detecting moment when we need to start output. 对于更快速的解决方案,您可能希望摆脱内循环,将其替换为逻辑以立即添加count[i]进行counter ,并检测何时需要开始输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM