java - 反转数组列表无法正常工作

标签 java arraylist reverse

我的任务是从输入文件test.txt中读取,该文本有一些句子。 我需要编写一个带有构造函数和三个方法的类。 其中之一必须颠倒句子中单词的顺序。

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

public class Reverser {


Scanner sc3 = null ;


//constructor takes input file and initialize scanner sc pointing at input
public Reverser(File file)throws FileNotFoundException, IOException{



 sc3 = new Scanner (file); 
}



//this method reverses the order of the words in each line of the input
//and prints it to output file specified in argument.
public void reverseEachLine(File outpr)throws FileNotFoundException, IOException{

     // ArrayList<String> wordsarraylist = new ArrayList<String>();

   while(sc3.hasNextLine()){
        String sentence = sc3.nextLine();
       // int length = sentence.length();
        String[] words =  sentence.split(" ");


       // wordsarraylist.clear();
        List<String> wordsarraylist = new ArrayList<String>(Arrays.asList(words));
        Collections.reverse(wordsarraylist);

        FileWriter writer = new FileWriter(outpr,true); 

        for(String str: wordsarraylist) {
            writer.write(str + " ");
           }

        writer.write(System.lineSeparator());
        writer.close();
    }


}


}

我已经删除了另外两种方法,但它们不会干扰这个方法。 这是我的主要内容:

import java.io.*;

public class DemoReverser {
    public static void main (String [] args)
        throws IOException, FileNotFoundException {
        Reverser r = new Reverser(new File("test.txt"));

        r.reverseEachLine(new File("out2.txt"));
    }
}

问题是在执行结束时我的输出文件包含相同的内容。这并不是颠倒顺序。怎么会? Collections.reverse() 不会颠倒顺序吗?所以当我打印它时,我应该把单词颠倒过来?

我还需要使用 arraylist。

这是我的输入文件:

This is just a small file. That
has some lines of text.
If we are successful, these
lines will be
reversed.
Let's hope for the best!

我应该在我的输出中得到这个:

That file. small a just is This
text. of lines some has
these successful, are we If
be will lines
reversed.
best! the for hope Let's

但是我得到了这个:

This is just a small file. That 
has some lines of text. 
If we are successful, these 
lines will be 
reversed. 
Let's hope for the best! 

最佳答案

尝试一下方法 reverseEachLine 的代码,它工作得很好。不要在构造函数中构造 Scanner

公共(public)类 MyReverser {

private File inputFile;

public MyReverser(File file) {
    this.inputFile = file;
}

public void reverseEachLine(File outpr) throws FileNotFoundException, IOException {
    Scanner sc = new Scanner(inputFile);

    ArrayList<List<String>> wordsarraylist = new ArrayList<List<String>>();
    while (sc.hasNextLine()) {
        String sentence = sc.nextLine();

        List words = Arrays.asList(sentence.split(" "));
        Collections.reverse(words);
        wordsarraylist.add(words);
    }

    FileWriter writer = new FileWriter(outpr, false);
    for (List<String> list : wordsarraylist) {
        for (String string : list) {
            writer.append(string + " ");
        }
        writer.append(System.lineSeparator());
    }
    writer.flush();
    writer.close();
}

}

我已经在这里回答了完整的代码 java cannot create file by 3 methods

关于java - 反转数组列表无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754227/

相关文章:

java - 让计分器像 Android 中的计时器一样工作

Java 正则表达式死于堆栈溢出 : need a better version

java - 我想声明名字全名和姓氏,但在此java程序中给出了错误

java - 将 IndexOf 与 arrayList 中的 customObject 一起使用

python - 反转列表中行的顺序

java - 如何反向遍历Linked Hash Map?

c - 反转字符串 C

java - 有哪些好的java图像处理工具或API?

java - 如何打印空字符串数组

java - 尝试从另一个方法调用 ArrayList