Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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 如何将字符串拆分为后缀数组?_Java_Arrays_Search_Phrase - Fatal编程技术网

Java 如何将字符串拆分为后缀数组?

Java 如何将字符串拆分为后缀数组?,java,arrays,search,phrase,Java,Arrays,Search,Phrase,将字符串拆分为后缀数组的最有效方法是什么 假设您有字符串“天气很好”,我想生成一个后缀数组,如下所示: [0] = "nice" [1] = "is nice" [2] = "weather is nice" [3] = "the weather is nice" 我可以以迭代器的形式从头到尾访问其标记(单词)上的字符串。使用Split在空格上拆分数组,然后从头到尾遍历生成的标记,取上一个后缀,并将当前标记前置到其前面。如果没有前置后缀,请使用空字符串: String str = "qu

将字符串拆分为后缀数组的最有效方法是什么

假设您有字符串“天气很好”,我想生成一个后缀数组,如下所示:

[0] = "nice"

[1] = "is nice"

[2] = "weather is nice"

[3] = "the weather is nice"

我可以以迭代器的形式从头到尾访问其标记(单词)上的字符串。

使用
Split
在空格上拆分数组,然后从头到尾遍历生成的标记,取上一个后缀,并将当前标记前置到其前面。如果没有前置后缀,请使用空字符串:

String str = "quick brown fox jumps over the lazy dog";
List<String> res = new ArrayList<String>();
String last = null;
String[] tok = str.split(" ");
for (int i = tok.length-1 ; i >= 0 ; i--) {
    if (last == null) {
        last = tok[i];
    } else {
        last = tok[i] + " " + last;
    }
    res.add(last);
}
for (String s : res) {
    System.out.println(s);
}

显而易见的解决方案是在空格上标记字符串,并将结果以相反顺序存储在
列表数组中。然后构建你的答案
listary
,从中,一点递归对灵魂有好处。

调用.split(“”);将返回字符串中的单词数组


它必须是一个数组吗?我想推荐一个
Iterable
,如果您愿意,可以向您展示如何使用
dog
lazy dog
the lazy dog
over the lazy dog
jumps over the lazy dog
fox jumps over the lazy dog
brown fox jumps over the lazy dog
quick brown fox jumps over the lazy dog