反转arraylist不能正常工作

时间:2015-09-24 05:49:53

标签: 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! 

1 个答案:

答案 0 :(得分:0)

尝试使用方法reverseEachLine的此代码,它可以正常工作。不要在构造函数中构造Scanner

公共类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