开发者

Java: variable length parameters as list for recursion

I would something like the following recursive method:

private Node getElementRec(Node currentNode, String ... names) {
    if (null == names || names.length == 0)
        return currentNode;
    else {
        Node child = currentNode.getChildWithName(names[0]);
        return ge开发者_StackOverflowtElementRec(child, namesAux.subList(1, names[1,]));
    }
}

Since variable length Java parameters (here names) are arrays, I cannot make something like names.sublist(1, names.size()) Although it would be rather inefficient, I've tried to convert the array to a list and then pass it to the method, but it doesn't accept a list

So the question is: is it possible in Java to do recursion over a variable length parameter (Type ... parameter)? Something like I showed is possible?

Thanks


You can use the copyOfRange function from the Arrays class to pass to the function

Arrays.copyOfRange(names, 1, names.length)

Another way to avoid copying would be to modify your function to accept start and end index:

private Node getElementRec(Node currentNode, int start, int end, String ... names) {
    if (null == names || start >= end || start < 0 || end > names.length)
        return currentNode;
    else {
        Node child = currentNode.getChildWithName(names[start]);
        return getElementRec(child, start+1, end, names);
    }
}


Instead of names.size() you can use names.length
Varags ... in java are arrays, not vectors as you are suggesting.


Pass the index

private Node getElementRec(Node currentNode, int i, String ... names) {
    if (null == names || names.length == i)
        return currentNode;
    else {
        Node child = currentNode.getChildWithName(names[i]);
        return getElementRec(child, i+1, names);
    }
}


String ... names is a shorthand of String[] names. So you can try:

else {
     Node child = currentNode.getChildWithName(names[0]);
     String[] remainingNames = new String[names.length -1];
     System.arrayCopy(names, 1, remainingNames, 0, names.length - 1);
     return getElementRec(child, remainingNames));
 }


You can pass array in place of vararg:

private Node getElementRec(Node currentNode, String ... names) {
     String slice[] = ..... // Get sub array from names
     ....
     getElementRec(node, slice);
     ....
}


First off: variable arity parameters are implemented using arrays and not as vectors (vector usually means java.util.Vector in Java, which would be a List implementation which would have a subList() method).

As you realized, collections (specifically List objects) allow much richer interactions than simple arrays. Luckily there's an easy way to transform an array into a List using Arrays.asList().

I'd implement the recursive method using List<String> instead of String... and provide a convenience method that takes a variable arity parameter:

private Node getElementRec(Node currentNode, String... names) {
    return getElementRec(currentNode, Arrays.asList(names));
}

private Node getElementRec(Node currentNode, List<String> names) {
    if (null == names || names.isEmpty())
        return currentNode;
    else {
        Node child = currentNode.getChildWithName(names.get(0));
        return getElementRec(child, names.subList(1, names.length()-1));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜