简体   繁体   English

返回字符串数组的递归函数(JAVA)

[英]Recursive function that return string array (JAVA)

I need some help in JAVA: I have a function signature which I can't change, and my function needs to be recursive and to return String array without any option to add it to the signature. 我在JAVA中需要一些帮助:我有一个函数签名,我无法更改,我的函数需要递归并返回String数组,没有任何选项将其添加到签名中。

This is the signature I've got: 这是我得到的签名:

public String[] findSimilar(String w, int index, int k)

The function looks for similar words in a TRIE structure, with a difference of K letters changes between them. 该函数在TRIE结构中查找相似的单词,它们之间的K字母变化不同。

For example- in a TRIE withe the words hello, nice, nine, cry, for the word "bike" and k=2, the function will return a String[] with nice and nine. 例如 - 在TRIE中,单词hello,nice,nine,cry,单词“bike”和k = 2,该函数将返回一个带有nice和9的String[]

I'm not looking for a solution, just for a method to return string array. 我不是在寻找一个解决方案,只是为了一个返回字符串数组的方法。

** I wrote a function with the signature I've received as a wrapper, but I just found out that I can't use wrapper. **我写了一个带有我作为包装器收到的签名的函数,但我发现我不能使用包装器。

Thank you! 谢谢!

I'm not looking for a solution, just for a method to return string array. 我不是在寻找一个解决方案,只是为了一个返回字符串数组的方法。

To return a string array with literals string1 and string2 you could just use an array initializer such as return new String[] { "string1", "string2"}; 要返回带有字符串string1string2的字符串数组,您可以使用数组初始值设定项,例如return new String[] { "string1", "string2"};

Else, you could just create the String array and assign values to its positions if you know beforehand how many elements you will be returning: 否则,如果您事先知道要返回的元素数量,则可以创建String数组并为其位置指定值:

String[] arr = new String[2];
arr[0] = "string1";
arr[1] = "string2";
return arr;

If it's the return type of a recursive function, you'll probably need to use the result from the recursive call to build your own result in the current call. 如果它是递归函数的返回类型,您可能需要使用递归调用的结果在当前调用中构建自己的结果。 Taking into account arrays cannot be extended, you'll need to create a new one with the expected size, and copy the values of the result into it for instance with System.arraycopy . 考虑到无法扩展数组,您需要创建一个具有预期大小的新数组,并将结果的值复制到其中,例如使用System.arraycopy

The trivial example: 琐碎的例子:

public String[] findSimilar(String w, int index, int k) {
    return new String[] {"string1","string2"}
}

Maybe more useful: 也许更有用:

public String[] findSimilar(String w, int index, int k) {
    List<String> similar = new ArrayList<>();
    // insert some implementation here

    return similar.toArray(new String[similar.size()]);
}

Use something like this. 使用这样的东西。 I would not like to provide full code just an idea 我不想提供完整的代码只是一个想法

public String[] findSimilar(String w, int index, int k) {
    String[] res1=findSimilar(conditions one);
    String[] res2=findSimilar(conditions two);

    String[] res=new String[res1.length+res2.length];
    //use public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    System.arraycopy(copyFrom, ..., copyTo, ..., ...);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM