【问题标题】:Reverse linkedlist and writing into a file: Java反向链表并写入文件:Java
【发布时间】:2014-03-06 21:30:46
【问题描述】:

我正在编写代码来读取文本文件,将其转换为 LinkedList,然后以相反的顺序写入另一个文本文件。

我面临的问题是在转换文本文件后,我无法反转我的列表。在写作时,我也面临着问题。这是我的代码,然后是它的输出......

package Collections;
import java.io.*;
import java.util.*;

public class Store implements Serializable {

    private static final long serialVersionUID = 1L;

    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws IOException {

        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter the File Name:");
        String fname1 = br1.readLine();
        FileInputStream fr1 = null;

        // Reading the file

        try {
            fr1 = new FileInputStream(fname1);
        } catch (FileNotFoundException fe) {
            System.out.println("File Not Found.");
            return;
        }
        BufferedInputStream bin1 = new BufferedInputStream(fr1);
        BufferedReader br = new BufferedReader(new InputStreamReader(bin1));
        LinkedList<String> words = new LinkedList<String>();
        FileOutputStream fout1 = new FileOutputStream("ReverseOrder.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fout1);

        // Converting the file into Linked List

        String theWord;
        while (true) {
            theWord = br.readLine();
            if (theWord != null && theWord.length() > 1)
                words.add(theWord);
            if (theWord == null)
                break;
        }
        String[] data = new String[words.size()];
        for (int i = 0; i < words.size(); i++) {

            data[i] = words.get(i);

        }
        System.out.println("Words:" + words);

        // Reversing the LInkedList... Here I don't know why
        // Collections.reverse(words) isn't working. I coudn't reverse my list

            Collections.reverse(words);

        System.out.println("Words:" + words);

        // Writing into a file....but here it is not printing in reverse order.
        // In fact if I tried to print in Regular order but it's printing with
        // extra characters

        oos.writeObject(words);
        oos.close();
        bin1.close();
    }
}

输出是:

Enter the File Name:replace.txt
Words:[Suresh is a good boy and He completed his graduation from IIT Guwahati in 2013 But he is a great man ]
Words:[Suresh is a good boy and He completed his graduation from IIT Guwahati in 2013 But he is a great man ]

文件中打印的输出是:

¬í sr java.util.LinkedList)S]J`^" xpw t eSuresh 是个好孩子,他于 2013 年从 IIT Guwahati 毕业,但他是个了不起的人 xq ~

【问题讨论】:

  • 你能提供你输入文件的内容吗?
  • Suresh 是个好孩子,他于 2013 年从 IIT Guwahati 毕业,但他是个了不起的人......这是我输入文件的内容
  • 都是一行写的吗?
  • 是的......它是用记事本写的......不仅是这个程序......我已经写入文本文件的其他程序(将对象发送到文本文件)正在显示一些ASCII码(不需要的字符)......你能帮我倒车吗......?
  • 您不是在反转单词,而是行尝试将每个单词写在新行中并检查是否有效

标签: java linked-list objectoutputstream bufferedinputstream


【解决方案1】:

这看起来不对:

for (int i = words.size() - 1; i > 0; i--) {
    words.add(words.get(i));
    Collections.reverse(words);
}

为什么要循环执行?如果Collections.reverse() 反转列表,你需要做的就是

Collections.reverse(words);

你应该完成了吗?

如果我在这里遗漏了什么,请告诉我

【讨论】:

    【解决方案2】:

    您正在循环中运行Collections.reverse(words);。您只需要运行一次并跳过循环并再次添加您自己的话。而不是这个:

        for (int i = words.size() - 1; i > 0; i--) {
    
            words.add(words.get(i));
            Collections.reverse(words);
        }
    

    只运行这个:

    Collections.reverse(words);
    

    【讨论】:

    • 您能分享一下您现在拥有的新代码吗? (更新问题)
    • 是的,我已经更新了代码,但输出是一样的......在新代码中,我在 Collections.reverse(words) 之前使用了 oos.writeObject(words) 但对象是打印到文件中的是 ASCII 码或其他东西....见输出
    【解决方案3】:

    要反转 LinkedList 遍历 LinkedList 将元素推送到 Stack 直到 LinkedList 的头部为空。然后从堆栈中弹出,将每个元素添加回您的linkedList,直到堆栈为空。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 1970-01-01
      • 2014-05-01
      相关资源
      最近更新 更多