1

I am practicing recursion and using a recursive helper method. In my helper method, an error appears saying that

The method someMethod(K) in the type Tree is not applicable for the arguments (K, List, int).

However, I do not want to use the someMethod(K k) method as I am trying to use the someMethod(K k, List<K> L, int n) helper method. How do I make Eclipse "know" that I'm trying to use the other method?

Here's what I have thus far:

public List<K> someMethod(K k) {
    List<K> L=new LinkedList<K>();
    if (lookup(k) != null) {
        return someMethod(k, L, 0);
    }
    return L;
}

private List<K> someMethod(K k, List<K> L, int n) {
    if (this.k.compareTo(k) == 0) {
        L.add(this.k);
        return list;
    }
    if (this.k.compareTo(k) < 0) {
        right.someMethod(k, L, n); //error here
        L.add(this.k);
    }
    if (this.k.compareTo(k) > 0) {
        left.someMethod(k, L, n); //error here
        L.add(this.k);
    }
}

Edit: declarations for left and right:

private Tree<K, V> left, right;
nem035
  • 34,790
  • 6
  • 87
  • 99
  • It this only an annotation from Eclipse or is the program actually not compiling? – Turing85 Nov 06 '14 at 00:57
  • Well, when I try to run the project it says that there are errors so I guess the program is actually not compiling because of that. I have no other errors. –  Nov 06 '14 at 01:00
  • Please show us the declarations of `left` and `right`. – ajb Nov 06 '14 at 01:02
  • 1
    Also, recursive helper methods that call themselves with the exact same parameters they're given have an annoying tendency to stack-overflow. – ajb Nov 06 '14 at 01:03
  • 1
    What's a `Tree`? Is that the class that includes `someMethod`? I think you need to post the whole thing. – ajb Nov 06 '14 at 01:12
  • I do not get an error on the lines you indicate, assuming everything else is declared suitably. You have probably done something else wrong in the rest of your code, but I can't see it. Also, as @Turing85 pointed out, your method needs to return something, but the presence or absence of the return doesn't affect whether I get any errors or warnings on those lines. – ajb Nov 06 '14 at 01:18

2 Answers2

0

Your helper-method is missing a final return-statement, which is probably messing up the syntax-check, ending in this strange error-message.

private List<K> someMethod(K k, List<K> L, int n) {
    if (this.k.compareTo(k) == 0) {
        L.add(this.k);
        return list;
    }
    if (this.k.compareTo(k) < 0) {
        right.someMethod(k, L, n); //error here
        L.add(this.k);
    }
    if (this.k.compareTo(k) > 0) {
        left.someMethod(k, L, n); //error here
        L.add(this.k);
    }
    return L; // change here, errors should be gone now
}
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • 1
    No. Missing `return` statements are not syntax errors. They are errors, but they will not be caught by the part of the compiler that cares about the syntax. – ajb Nov 06 '14 at 01:04
  • @ajb, yes they are. The compiler requires us to always return something if the return type isn't `void`. – nem035 Nov 06 '14 at 01:07
  • Please reread what I said carefully. They are errors, but they are not **syntax errors**. This sort of error will **not** confuse the compiler into putting an error on a different line. – ajb Nov 06 '14 at 01:10
  • One could argue about the kind of error. I think ajb is right, because the Parser performs return-statement checks on the AST and therefore it might be a semantical error. Nevertheless, this is/was the problem and it is fixed now =) – Turing85 Nov 06 '14 at 01:10
  • @ajb, sorry for not reading carefully. I see what you meant now. – nem035 Nov 06 '14 at 01:12
0

First problem I see in the second method is that you are only returning something in the case when the statement

if (this.k.compareTo(k) == 0)

is true.

The compiler should give you an error because your method is declared to return List<K>

private List<K> someMethod(K k, List<K> L, int n)

To solve this, you should return something either in each if statement, or at the bottom of your method. Based on your logic, you want to return an error value when no matches satisfying the above if statement are found. Thus, for example, you could return null by putting this statement at the bottom of your method:

return null;

Or, if you don't want to deal with null values, return an empty list:

return new ArrayList<K>();

If you make this change your code compiles fine on my machine.

Here is an Ideone example that compiles fine with the change I suggested.

Furthermore, as @ajb mentioned in the comments, you aren't really taking care of the base case for your recursion.

Meaning, you are not changing the arguments:

K k, List<K> L, int n 

of your recursive method when you pass them through recursion and thus will end up in an "infinite" recursion causing StackOverFlowError in the case where no elements satisfy the condition

if(this.k.compareTo(k) == 0) {
    L.add(this.k);
    return list;  // this returns from recursion but nothing else
}

You should define some sort of a base case that will stop the recursion no matter if you find matches or not.

nem035
  • 34,790
  • 6
  • 87
  • 99