Reputation: 95
I have created a method that I believe to be recursive.
public AssaultTeam getTeam(String teamName) {
for(AssaultTeam team : teams){
if(team.getName().equals(teamName)){
return team;
}
}
AssaultTeam newTeam = new AssaultTeam(teamName);
teams.add(newTeam);
return getTeam(teamName);
}
'teams' is an ArrayList of AssaultTeam
I have never used recursion before and I am not sure if this method will work.
Upvotes: 0
Views: 131
Reputation: 1326
Would it work as in run? Yes.
Would it do what you want it to do? No.
Objects (like a String) needs to be compared with .equals in Java. As pointer out by Nambari.
Is it recursive? Technically yes since it calls itself.
Is it necesary for it to be recursive? No. You know what it should return so you should just return it.
You want to do something like this:
public AssaultTeam getTeam(String teamName){
for(AssaultTeam team : teams){
if(team.getName().equals(teamName)){
return team;
}
}
AssaultTeam newTeam = new AssaultTeam(teamName);
teams.add(newTeam);
return newTeam;
}
EDIT: I think I it will be beneficial to explain how you can use recursion.
Recursion is defined by two things.
Say you have a 3x3 square filled with numbers
1 2 3 4 5 6 7 8 9
And you want to start at top left corner and you can either move right or down and you want to end at the bottom right corner. If you add the sum total of each number in the squares you stepped on you want to know the maximum possible;
Some pseudo code
int recurse(x,y)
int max = 0;
if can move right
max = recurse(x+1,y)
if can move down
int tmp = recurse(x,y+1)
if tmp greater than max
max = tmp;
return square[x][y] + max;
Just a silly example of where you would use recursion. Hopefully this helped
Upvotes: 0
Reputation: 103787
Is it recursion? I suppose it technically is, because the method has a base case and otherwise defers back to itself.
However, it's not a particularly great example of recursion because there are only ever two cases. And in the second case (where the team wasn't present), the recursive call will always return immediately. So as others have said, it would make much more sense to replace the last line with return newTeam
, because this is entirely equivalent (and much simpler/easier to understand).
And on a slightly more esoteric point, recursion tends to be associated with a functional programming style, which generally avoids mutable variables. Typically recursive methods will call the method again with different arguments, and the method will act like a function, i.e. will always return the same result given the same input. Technically there is no reason why you couldn't have recursion and mutability, but speaking personally it would break my preconceptions and confuse me. (Much in the same way that you could call a method add
if it took two ints and returned their maximum; it technically works but it'll cause people to do a double-take.)
If it helps, here's a classic example of recursion - seeing whether a list of Strings contains a given element:
public boolean recContains(String elem, List<String> list) {
if (list.isEmpty())
return false;
else if (list.get(0).equals(elem))
return true;
else
return recContains(elem, list.subList(1, list.length()));
}
Notice that:
These might not be technically required for a proper recursive method, but they're definitely the sort of properties I would associate with them.
Upvotes: 0
Reputation: 4568
Yes this is recursion, recursion is when invoke the method itself and you do it in the end of this method by the code return getTeam(teamNames);
Yes, it will work but in a little weird way, you actually dont need recursion for this solution
public AssaultTeam getTeam(String teamName) {
//iterate throught the teams list - OK
for(AssaultTeam team : teams){
if(team.getName().equals(teamName)){
//if found - OK
return team;
}
}
AssaultTeam newTeam = new AssaultTeam(teamName);
teams.add(newTeam);
//call the getTeam, which will cause another iteration to find the item - NOT GOOD
//return getTeam(teamName);
//just return item you just created
return newTeam;
}
Upvotes: 3
Reputation: 6755
It is recursive because the method calls itself. It would not work because of the ==
comparison, as mentioned in one of the comments. You could use the String#equals()
method to fix that problem. However, the recursion is unnecessary. You could just return newTeam
.
Upvotes: 2
Reputation: 426
In recursion, the method returns the desired result (direct case) or call itself with a simpler problem (recursive case).
In your case, the direct case could be team.getName().equals(teamName)
or that you arrive at the end of the array, then you can return team
.
Your recursive case could be a call to check next element in teams array.
public AssaultTeam getTeam(int index, String teamName)
{
AssaultTeam team = teams.get(index);
if (team.getName().equals(teamName))
{
return team;
}
else if (index == teams.size())
{
AssaultTeam newTeam = new AssaultTeam(teamName);
teams.add(newTeam);
return team;
}
else
{
++index;
return getTeam(index, teamName);
}
}
Upvotes: 1