【问题标题】:Why is the smallest number read from my file always 0? (Java)为什么从我的文件中读取的最小数字总是 0? (爪哇)
【发布时间】:2016-02-16 12:46:27
【问题描述】:

当我运行程序时,最小的数字always原来是0。这是为什么呢?最大值和平均值似乎是正确的。

问题很可能在于我如何使用随机类。

import java.io.*;
import java.util.*;
import java.util.Arrays;

public class ReadAndWrite {
public static void main(String[] args) {
    Random ran = new Random();
    int i_data = 0;
    int[] sort = new int[100];
    File file = new File("Test4");
    int total = 0;
    int average = 0;    

    try {
    file.createNewFile();
    }       
    catch(Exception e) {
        System.out.println("Could not create the file for some reason. Try again.");
        System.exit(0);
    }

    try {
        FileOutputStream fos = new FileOutputStream("Test4");
        ObjectOutputStream oos = new ObjectOutputStream(fos);

        for(int i = 0; i <= 100; i++) {
          int x = ran.nextInt(100);
          oos.writeInt(x);
        }
        oos.close();
    }
    catch(IOException e) {
        System.out.println("Whoops");
    }

    try {           
        FileInputStream fos = new FileInputStream("Test4");
        ObjectInputStream ooss = new ObjectInputStream(fos);            

        for (int i = 0; i < 100; i++) {
            sort[i] = ooss.readInt();

        }
        Arrays.sort(sort);
        for(int i = 0; i < 100; i++) {
            total = total + sort[i];
            average = total/100;
        }


        System.out.println("The largest number in the file is: " + sort[99]);
        System.out.println("The smallest number in the file is: " + sort[0]);
        System.out.println("The average number in the file is: " + average);

        ooss.close();
    }
    catch(Exception e) {
        System.out.println(e);
    }   
  }
}

【问题讨论】:

  • 为什么总是加一个数后排序?
  • 你为什么要打电话给Arrays.sort() 100 次。也可能因为nextInt(100) 而获得0
  • 好的,我已经多次运行该程序,最后得到了一个 1 作为我的最小值。 @Satya 感谢您指出这一点。我错误地将 Arrays.sort() 放入循环中。

标签: java file random file-io


【解决方案1】:

您在读取每个值时对数组进行排序。

 for(int i = 0; i < 100; i++) {
     sort[i] = ooss.readInt();
     Arrays.sort(sort);
 }

这意味着你从 [1, 0, 0, 0, ...] 开始,但排序后你有 [0, 0, 0, ... 1 ]

这是调试器帮助您调试程序的地方。解决方案是仅在读取数组后对其进行排序。

一个更简单的解决方案是一次性写入和读取数组,而不是使用循环。

顺便说一句:除非您正在编写/读取对象,否则您不需要使用 ObjectOutputStream 并且与说 DataOutputStream 相比它具有开销。

正如@KevinEsche 指出的那样,如果您有 100 个 0 到 99 的随机值,那么其中一个很可能是 0,尽管并非每次都如此。

更短的实现可能如下所示

public static void main(String[] args) throws IOException {
    Random rand = new Random();

    int samples = 100;
    try (DataOutputStream out = new DataOutputStream(new FileOutputStream("test"))) {
        out.writeInt(samples);
        for (int i = 0; i < samples; i++)
            out.writeInt(rand.nextInt(100));
    }

    int[] sort;
    try (DataInputStream in = new DataInputStream(new FileInputStream("test"))) {
        int len = in.readInt();
        sort = new int[len];
        for (int i = 0; i < len; i++)
            sort[i] = in.readInt();
    }

    IntSummaryStatistics stats = IntStream.of(sort).summaryStatistics();
    System.out.println("The largest number in the file is: " + stats.getMax());
    System.out.println("The smallest number in the file is: " + stats.getMin());
    System.out.println("The average number in the file is: " + stats.getAverage());
}

【讨论】:

  • 我会在当前实现中添加 Random 它仍然很可能将 0 作为最小数字
  • @Thomas 一半的值最终会变为 0,因此差异很大。
  • 嗯,我误解了你的回答。你是对的,在第一次排序后元素 0 的值将是 0,他会将下一个值读入元素 1,从而有效地覆盖一半的值。
猜你喜欢
  • 2015-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
  • 2013-04-07
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多