Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 反转arraylist无法正常工作_Java_Arraylist_Reverse - Fatal编程技术网

Java 反转arraylist无法正常工作

Java 反转arraylist无法正常工作,java,arraylist,reverse,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(

我的任务是从输入文件
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();
    }


}


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

我还需要使用arraylist

这是我的输入文件:

这只是一个小文件
有几行文字。
如果我们成功了,这些
线路将是
颠倒的。
让我们期待最好的结果!
我应该在我的输出中得到这个:

那个文件。小a就是这个
文本。有几行
这些都是成功的吗
这是我的遗嘱
颠倒的。
最好的!希望,让我们
但我明白了:

这只是一个小文件
有几行文字。
如果我们成功了,这些
线路将是
颠倒的。
让我们期待最好的结果!

尝试此代码的方法
reverseEachLine
,效果很好。不要在构造器处构造
扫描仪

公共类反向器{

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();
}
私有文件输入文件;
公共MyReverser(文件){
this.inputFile=文件;
}
public void reverseEachLine(文件输出)抛出FileNotFoundException、IOException{
扫描仪sc=新扫描仪(输入文件);
ArrayList wordsarraylist=新建ArrayList();
while(sc.hasNextLine()){
字符串语句=sc.nextLine();
List words=Arrays.asList(句子.split(“”);
收藏。反面(文字);
单词列表。添加(单词);
}
FileWriter writer=新的FileWriter(输出,false);
for(列表:wordsarraylist){
用于(字符串:列表){
writer.append(字符串+“”);
}
writer.append(System.lineSeparator());
}
writer.flush();
writer.close();
}
}


我在这里回答了完整的代码

请尝试使用此代码的方法reverseEachLine,效果很好。不要在构造器处构造
扫描仪

公共类反向器{

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();
}
私有文件输入文件;
公共MyReverser(文件){
this.inputFile=文件;
}
public void reverseEachLine(文件输出)抛出FileNotFoundException、IOException{
扫描仪sc=新扫描仪(输入文件);
ArrayList wordsarraylist=新建ArrayList();
while(sc.hasNextLine()){
字符串语句=sc.nextLine();
List words=Arrays.asList(句子.split(“”);
收藏。反面(文字);
单词列表。添加(单词);
}
FileWriter writer=新的FileWriter(输出,false);
for(列表:wordsarraylist){
用于(字符串:列表){
writer.append(字符串+“”);
}
writer.append(System.lineSeparator());
}
writer.flush();
writer.close();
}
}


我已经用完整的代码回答了这里的问题

请显示示例输入、示例输出、预期输出,并在理想情况下修复您的代码格式-缩进现在就在这里。(顺便说一句,不清楚为什么每次迭代都要重新创建输出文件-为什么不让编写器一直打开?)顺便说一下,代码对我来说是有效的。有了一个“first second”和“third fourth”两行的输入文件,我得到了一个“second first”和“third third”的输出文件,这和预期的完全一样。我也试过了。它也适用于我。检查代码,它会根据需要提供预期的输出。请显示示例输入、示例输出、预期输出,并在理想情况下修复代码格式-缩进现在就在这里。(顺便说一句,不清楚为什么每次迭代都要重新创建输出文件-为什么不让编写器一直打开?)顺便说一下,代码对我来说是有效的。有了一个“first second”和“third fourth”两行的输入文件,我得到了一个“second first”和“third third”的输出文件,这和预期的完全一样。我也试过了。它也适用于我。检查代码,它会根据需要提供预期的输出。