为什么 Collections.shuffle(List) 之后会出现空行?

发布于 2024-12-26 04:32:44 字数 1329 浏览 1 评论 0原文

我正在编写一些 Java 代码来将数据文件的每一行读取到数组列表中,对该列表进行混洗,然后对混洗后的数据执行一些进一步的操作。问题是,在洗牌之后,我得到了数组列表中某些元素的空元素。

//this file contains the data
String input = ...;
//this file contains the number of rows in the data file
String input2 = ...;
FileInputStream fstream2 = new FileInputStream(input2);
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader brIndex = new BufferedReader(new InputStreamReader(in2));

for(int i = 1; i <= N; i++) {
        FileInputStream fstream = new FileInputStream(input+(i+1)+".txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader brData = new BufferedReader(new InputStreamReader(in));
        num = Integer.parseInt(brIndex.readLine());

        ArrayList<String> temp = new ArrayList<String>();
        for(int j = 0; j < num; j++) {
            temp.add(brData.readLine());
        }
        brData.close();
        Collections.shuffle(temp);

        //read in shuffled data
        for(int j = 0; j < num; j++) {
            rowData = temp.get(j); ...
}

在执行代码时,在使用 rowData 时,我收到了 NullPointerException 。原始文件有 17,169 行(没有一行为空)。 temp.size() 也是 17,169,但是当我将 temp 打印到控制台时,temp.get(j) 对于某些 j 为 null而不是为了其他人。

谁能向我解释为什么会出现这种情况以及如何避免这种情况?

I am writing some Java code to read each line of a data file into an array list, shuffle that list, and then perform some further operations on the shuffled data. The problem is that, after shuffling, I am getting null elements for some of the elements in the array list.

//this file contains the data
String input = ...;
//this file contains the number of rows in the data file
String input2 = ...;
FileInputStream fstream2 = new FileInputStream(input2);
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader brIndex = new BufferedReader(new InputStreamReader(in2));

for(int i = 1; i <= N; i++) {
        FileInputStream fstream = new FileInputStream(input+(i+1)+".txt");
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader brData = new BufferedReader(new InputStreamReader(in));
        num = Integer.parseInt(brIndex.readLine());

        ArrayList<String> temp = new ArrayList<String>();
        for(int j = 0; j < num; j++) {
            temp.add(brData.readLine());
        }
        brData.close();
        Collections.shuffle(temp);

        //read in shuffled data
        for(int j = 0; j < num; j++) {
            rowData = temp.get(j); ...
}

In executing the code, I get a NullPointerException after this when working with rowData. The original file has 17,169 rows (none of them are null). temp.size() is also 17,169 but when I printed temp out to the console, temp.get(j) was null for some j and not for others.

Can anyone explain to me why this is the case and how to avoid it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扶醉桌前 2025-01-02 04:32:44

问题可能是文件中没有足够的行作为 num

尝试:

List<String> temp = new ArrayList<String>();
String line;
while((line = br.readLine()) != null) {
    temp.add(brData.readLine());
}

您还可以在实际随机播放列表之前检查列表中是否有空元素,因为随机播放不会向列表中插入任何新元素。

The problem might be there are not enough lines in the file as num.

Try:

List<String> temp = new ArrayList<String>();
String line;
while((line = br.readLine()) != null) {
    temp.add(brData.readLine());
}

You could also check for null elements in the list before you actually shuffle the list, as shuffle does not insert any new element to the list.

画▽骨i 2025-01-02 04:32:44

您从不检查 readLine() 是否返回 null。检查一下,您会发现您可能读到了文件末尾。

此外,读取器、写入器和流应始终在finally 块中关闭。

You never check if readLine() returns null or not. Check it, and you'll discover that you probably read past the end of the file.

Also, readers, writers and streams should always be closed in a finally block.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文