【问题标题】:Find max and min from file loop从文件循环中查找最大值和最小值
【发布时间】:2016-06-07 15:05:09
【问题描述】:

我需要从这个文件中找到最大和最小整数我在文件中写了一个 for 循环现在我需要一种使用文件流读取最大和最小的方法

  public static void main(String[] args) throws FileNotFoundException, IOException {
    // TODO code application logic here
    PrintWriter file = new PrintWriter("numbers.dat");
    FileInputStream in = new FileInputStream("numbers.dat");

    DataInputStream data = new DataInputStream(in);

    try (DataOutputStream output = new DataOutputStream(new FileOutputStream("numbers.dat"))) {
        for (int i = 0; i < 1000; i++) {
            output.write(i);

        }
        output.close();

    }

【问题讨论】:

  • 你的问题是什么,这段代码是做什么的,你能解释一下吗?

标签: java file int max min


【解决方案1】:

为了响应“我需要从这个文件中找到最大和最小整数”,只需使用 for 循环来查找最大和最小数字。

int[] integers;  // Your integers in an array, however you loaded them.
int max;         // To hold the maximum integer found.
int min;         // To hold the minimum integer found.

    for (int element : integers) {
        // If it's bigger, update.
        if (element > max) {
            max = element;
        }

        // If it's smaller, update.
        if (element < min) {
            min = element;
        }
    }
}

为了响应“我现在向文件编写了一个 for 循环,我需要一种使用文件流读取最大值和最小值的方法”,您必须将文件加载到内存中。

现在,我不知道您将如何使用打印文件的方式(或者至少,您打算如何在给定的代码中编写文件)而不使用任何分隔符(例如,\n)。

【讨论】:

    【解决方案2】:

    我建议将文件解析为整数列表。

    如果您将文件解析为整数列表,您可以通过以下方式获得最小值和最大值:

    List<Integer> l = new ArrayList<Integer>();
    // Populate the list from the entries in the file
    ...
    // Get the min and max
    Integer min = Collections.min(l);
    Integer max = Collections.max(l);
    System.out.println("min=" + min + "; max=" + max);
    

    要确定如何从文件中创建整数列表,了解文件的格式会很有帮助。

    【讨论】:

    • 请原谅我的无知,但我不确定所有“dat”文件的内容是否相同。如果您在文本编辑器中打开它,内容是什么样的?
    【解决方案3】:

    查看您的代码,您需要调用输出。writeInt(i) 不是 write(i)

    private static void doRead() throws IOException
    {
        final PrintWriter file = new PrintWriter("numbers.dat");
    
        try (DataOutputStream output = new DataOutputStream(new FileOutputStream("numbers.dat"))) {
            for (int i = 0; i < SIZE; i++) {
                output.writeInt(i);  // do writeInt(i) not write(i)
            }
        }
    
        try ( FileInputStream in = new FileInputStream("numbers.dat");
              DataInputStream data = new DataInputStream(in) )
        {
    
            final ArrayList<Integer> list = new ArrayList<>(SIZE);
    
            for( int i=0; i < SIZE; i++)
            {
                list.add( data.readInt() );
            }
    
            final int max = list.stream()
                .max( (a, b) -> Integer.compare( a, b ) )
                .get();
    
            final int min = list.stream()
                .min( (a, b) -> Integer.compare( a, b ) )
                .get();
    
            System.out.println("max=" + max);
            System.out.println("min=" + min);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-10
      • 2022-09-29
      • 2018-09-21
      • 2013-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2018-08-17
      • 2019-09-18
      相关资源
      最近更新 更多