返回java中的一组字符串

返回java中的一组字符串,java,string,recursion,set,Java,String,Recursion,Set,首先,我想道歉,因为我的英语不好。现在,如何使用递归返回一组字符串?我知道逻辑,但我不知道如何在从字符串中添加所有这些单词后返回集合 使用如下字符串的示例 String s = "how do i return this?"; 在将这个字符串传递到方法中之后,我需要创建一个集合,然后使用递归删除该字符串(该字符串将被添加到集合中,并返回集合) 当集合返回时,我应该将集合输出到一个文件中 我的逻辑是: //because every word in the string ends with a

首先,我想道歉,因为我的英语不好。现在,如何使用递归返回一组字符串?我知道逻辑,但我不知道如何在从字符串中添加所有这些单词后返回集合

使用如下字符串的示例

String s = "how do i return this?";
在将这个字符串传递到方法中之后,我需要创建一个集合,然后使用递归删除该字符串(该字符串将被添加到集合中,并返回集合)

当集合返回时,我应该将集合输出到一个文件中

我的逻辑是:

//because every word in the string ends with a space
//So I need to get the index of that space
int n = s.indexOf(' ');
//add each word to the set (each word start at index 0 to the index of the space ' '.
set.add(s.substring(0, n));
//recursive case ( keep doing this until the length of string is 0.)
methodName(n+1, s.length());

我知道我可以用类变量(set)实现这一点,但在这种情况下我需要使用local,它似乎不起作用。

要从方法返回任何内容,请定义方法如下:

<return type> methodName(parameters) {
    // code
    return retval;
}
最后,添加一个return语句:

return set;

完成。

将集合添加到方法的参数:

methodName(n+1, s.length(), set);

你在正确的轨道上

我的建议

  • 使用LinkedHashSet作为集合实现,这样就可以按照插入令牌的顺序获取令牌
  • 递归方法必须将集合作为参数传递(以便它可以累积令牌)
  • 不要忘记在递归方法中添加一种停止递归的方法(例如,如果索引在字符串末尾)
我解决这个问题的方法是

private static void recursiveMethod(String x, Set<String> s)
private static void recursiveMethod(字符串x,集合s)

其中x是前一个字符串的子字符串。

您需要一个基本情况来知道在哪里完成递归,实际上使用indexOf是一个很好的选择,如果它是-1,那么您就无事可做了

例如:

import java.util.HashSet;
import java.util.Set;

public class RecursionExample {

    public static void main(String[] args) {
       Set<String> set = new HashSet<>();
       methodName("how do i return this?", set);
       System.out.println(set);
   }

   static void methodName(String s, Set<String> set) {
    // because every word in the string ends with a space
    // So I need to get the index of that space
    int n = s.indexOf(' ');
    // base case
    if (n < 0) {
        set.add(s);
        return;
    }
    // add each word to the set (each word start at index 0 to the index of
    // the space ' '.
    set.add(s.substring(0, n));
    // recursive case ( keep doing this until the length of string is 0.)
    methodName(s.substring(n + 1, s.length()), set);
}
}

令人惊叹的。难怪它不起作用。。我使用set.add(methodName(n+1,s.length());基本上我知道其他的一切,只是我没有使用addAll,谢谢你指出。但在这种情况下,我只能使用charAt、length、indexOf、substring。不允许使用其他方法,也不允许使用循环。好的!我给你举了一个例子,没有使用带递归调用的split。谢谢你的努力。但是我把问题解决了。我已经准备好了所有的代码,只是我没有在递归中使用addAll。(我使用了add,它给了我一系列错误)。您仍然可以使用
add
而不使用
addAll
将集合作为参数传递,我将编辑我的示例
private static void recursiveMethod(String x, Set<String> s)
import java.util.HashSet;
import java.util.Set;

public class RecursionExample {

    public static void main(String[] args) {
       Set<String> set = new HashSet<>();
       methodName("how do i return this?", set);
       System.out.println(set);
   }

   static void methodName(String s, Set<String> set) {
    // because every word in the string ends with a space
    // So I need to get the index of that space
    int n = s.indexOf(' ');
    // base case
    if (n < 0) {
        set.add(s);
        return;
    }
    // add each word to the set (each word start at index 0 to the index of
    // the space ' '.
    set.add(s.substring(0, n));
    // recursive case ( keep doing this until the length of string is 0.)
    methodName(s.substring(n + 1, s.length()), set);
}
}
[how, return, do, this?, i]