如何在Java中分解字符串数组列表中的单词,然后反转它们



标题听起来可能令人困惑。

简单地说,我有一个文件里面有文本。我用扫描仪读取了文本。我将该文本存储到一个数组列表中,该列表是一个字符串。我遇到的问题是,我需要制作一个方法来反转ArrayList中的文本。文本确实相反,但并非完全相反。我也需要单词按相反的顺序排列,但我的字符串排列列表被分解成了句子。

因此,我需要帮助访问每个ArrayList,并将这些单词分解为其他",然后将它们放在我的方法中,这样它就可以正确地反转这些单词。

我想完成的是先把台词拆开,把单词倒过来。困难的部分仍然是逐行返回全文。我的教授建议创建辅助方法来解决这个问题。请帮忙。

package thePackage;
/**
* Adan Vivero
* 12.6.18
* In this lab, I will be working with classes, 
ArrayLists, and files 
* mainly.
*/
import java.io.File;
import java.util.ArrayList;
import java.io.*;
import java.util.*;
/**
* In this class, we create two private variables which is a File and 
* ArrayList that is a String that will be
* initialized later on in the code.
*/
public class FileManipulator
{
private File inputFile;
private ArrayList <String> words;
/**
* In this method, we are initializing the File and Arraylist and 
* having a Scanner reach a file to grab the text
* and insert it into the ArrayList as a String.
* @param fileName is a type String that the user in the other class 
* "FileDriver" types in and it reads the file.
* @throws FileNotFoundException if the file does not exist.
*/
public FileManipulator(String fileName) throws FileNotFoundException
{
inputFile = new File(fileName);
words = new ArrayList<String>();
Scanner fileWords = new Scanner(inputFile);
while(fileWords.hasNextLine())  /** This will read the file and 
continue to loop as long as there are still more lines left to read 
from the file. */
{
String lineScan = fileWords.nextLine(); /** I created a String 
called LineScan that will read the whole line of the file as a String 
*/
words.add(lineScan);    /** Right here, we add whatever the 
String LineScan read, into the String ArrayList. (It adds in lines, 
not individual words.) */
}
System.out.println(words.size() + " ");
}
/**
* In this method, the goal is to reverse everything that is in the 
ArrayList "words". It is flawed at the moment since
* it correctly returns the lines in reverse order, but the words in 
each line are not reversed. 
*/
public void reverse()
{
for(int i = words.size()-1; i >= 0; i--) /** This for loop reverses 
*each of the lines in reverse order. */
{
System.out.println(words.get(i) + " ");
}
for(int i = 0; i < words.size(); i++)
{
//System.out.print(words.get(i) + " ");
}
}
public void removePlurals()
{
for(int i = words.size()-1; i >= 0; i--)
{
String plurals = words.get(i);
if(plurals.endsWith("s"))
{
words.remove(i);
}
System.out.println(words.get(i) + " ");
}
}
public void stutter(int k)
{
k = 3;
for(int i = words.size()-1; i <= words.size()-1; i++)
{
System.out.println(words.get(i));
}
}
}

当前输出:

when tweetle beetles fight , 
it's called a tweetle beetle battle .
and when they battle in puddles , 
they are tweetle beetle puddle battles .
and when tweetle beetles battle with paddles in puddles , 
they call them tweetle beetle puddle paddle battles .
and ...
when beetles battle beetles in  puddle paddle battles 
and the beetle battle puddles are puddles in  bottles ...
... they call these tweetle beetle bottle puddle paddle battle muddles .
and ...
when beetles fight these bottle battles with their paddles 
and the bottles are on poodles and the poodles are eating noodles ...
... they call these muddle puddle tweetle poodle beetle noodle 
bottle paddle battles .

所需输出:

. battles paddle bottle
noodle beetle poodle tweetle puddle muddle these call they ...
... noodles eating are poodles the and poodles on are bottles the and
paddles their with battles bottle these fight beetles when
... and
. muddles battle paddle puddle bottle beetle tweetle these call they ...
... bottles in puddles are puddles battle beetle the and
battles paddle puddle in beetles battle beetles when
... and
. battles paddle puddle beetle tweetle them call they
, puddles in paddles with battle beetles tweetle when and
. battles puddle beetle tweetle are they
, puddles in battle they when and
. battle beetle tweetle a called it's
, fight beetles tweetle when

如果你想保留句子,但每个句子中的单词都颠倒过来,你可以这样做:

while(fileWords.hasNextLine()) {
StringBuilder sb = new StringBuilder();
String[] wordsArr = fileWords.nextLine().split("\s+");
for(String str: wordsArr) {
sb.insert(0, str).insert(str.length(), " ");
}
words.add(sb.toString());
}
Collections.reverse(words);
words.forEach(System.out::println);

如果您试图以相反顺序获得单词的List

问题是,您在List中添加行,然后反向打印这些行,但不打印单词。您可以通过在空白处拆分行并将所有这些标记添加到List中来轻松解决此问题。您可以使用split()方法:

for(String str : lineScan.split("\s+")) {
words.add(str);
}

然而,使用Collections.addAll:,我们可以更优雅地做到这一点

Collections.addAll(words, lineScan.split("\s+")); 

或者当你打印时,你可以在那里把它分成几个单词:

for(int i = words.size()-1; i >= 0; i--) 
{
String[] wordArr = words.get(i).split("\s+");
for(int j = wordArr.length - 1; j >= 0; j--) {
System.out.println(wordArr[j]);
}
}

或者java 8+:

BufferedReader br = new BufferedReader(new FileReader(fileName));
List<String> list = br.lines()
.map(e -> e.split("\s+"))
.flatMap(lineArr -> Arrays.stream(lineArr))
.collect(Collectors.toList());
Collections.reverse(list);

它将使用lines()方法获取行流,将它们拆分为单词,反转它们,然后将它们收集到List,然后反转它们

我想明白了。我在这里得到了一些帮助,但我把它移到了相反的方法上。

public class FileManipulator
{
private File inputFile;
private ArrayList <String> words;
/**
* In this method, we are initializing the File and Arraylist and having a 
Scanner reach a file to grab the text
* and insert it into the ArrayList as a String.
* @param fileName is a type String that the user in the other class 
"FileDriver" types in and it reads the file.
* @throws FileNotFoundException if the file does not exist.
*/
public FileManipulator(String fileName) throws FileNotFoundException
{
inputFile = new File(fileName);
words = new ArrayList<String>();
Scanner fileWords = new Scanner(inputFile);
while(fileWords.hasNextLine())
{
StringBuilder sb = new StringBuilder();
String[] wordsArr = fileWords.nextLine().split("\s+");
for(String str: wordsArr)
{
sb.insert(0, str).insert(str.length(), " ");
}
words.add(sb.toString());
}
}
/**
* In this method, the goal is to reverse everything that is in the ArrayList 
"words". It is flawed at the moment since
* it correctly returns the lines in reverse order, but the words in each line 
are not reversed.
*/
public void reverse()
{
for(int i = words.size()-1; i >= 0; i--) // This for loop reverses each of 
the lines in reverse order.
{
System.out.println(words.get(i) + " ");
}
}

(请举例说明。(

我想把单词分开,然后颠倒它们的顺序。

在这种情况下,请尝试使用String.split("(生成一个char数组。即:

String sentence = "This is my very poorly made answer.";
String [] words2 = sentence.split(" ");
String [] words = new int [words.length];
for(int i=words.length; i<=0; i--){
words[words.length - i] = words2[i];
}

此外,java库中可能还内置了其他方法,可以满足您的要求。

相关内容

最新更新