Problem with my Recursive function
Hi,
I am trying to loop over and search for a specific path.
I use recursion to go over all the folders, get the path to the root, and compare the folder's path to the path string I sent.
Something here is not working properly.
I will be happy if someone can have a second look to see what went wrong:
this.childFolder = stcmd.getSpecifiedFolder(childFolderPath,v1);
protected Folder getSpecifiedFolder (String path ,String viewName){
/* Search for StarTeam folder using specific path*/
View tempView = getView(viewName);
Folder rootFolder = tempView.getRootFolder();
if (rootFolder.getPath().equals(path)){
return rootFolder;
}
else{
Folder folder = getFolder(rootFolder,path);
return folder;
}
}
private Folder getFolder(Folder rootFolder, String path){
Folder[] folderList = rootFolder.getSubFolders();
for (int i = 0 ; i < folderList.length ; i++){
String currentPath = folderList[i].getFolderHierarchy();
if (currentPath.equals(path)){
return folderList[i];
}
else{
getFolder(folderList[i],path);//Recursion.
}
}
returnnull;
}
Hi Sorry.
1) Well the code is OK and running.
2) Yes, I am using a debugger.
3) I am not using System.out....
4) It appears that the recursion is NOT running all over the tree. It is going down the first leaf and ends.
for example if I have a root folder: A
and under there are two sub folders B,C
I am going down the B tree and leave the C folder unhandled.
Hey, where is the help that I am looking for....
So I have changed the functions, look at them
protected Folder getSpecifiedFolder (String path ,String viewName){
/* Search for StarTeam folder using specific path*/
View tempView = getView(viewName);
Folder rootFolder = tempView.getRootFolder();
if (rootFolder.getPath().equals(path)){
return rootFolder;
}
else{
Folder[] folderList = rootFolder.getSubFolders();
Folder folder = getFolder(folderList[0],0,path);
return folder;
}
}
private Folder getFolder(Folder subFolder, int i, String path){
String tempPath = subFolder.getFolderHierarchy();
if (tempPath.equals(path)){
return subFolder;
}
//if (subFolder.getSubFolders()[i] == null){
if (subFolder.getSubFolders().length == 0){
return null;
}
if (subFolder.getSubFolders().length > 0) {
i = 0;
}
else{
i++;
}
return getFolder(subFolder.getSubFolders()[i] , i, path);