Update 3: I found it.
options[i].revmdp(route+dirrections[i],num);
Here you are doing string concatenation on an array and a String. This causes to set route[num]
in the level of recursion to this concat result.
質問
I'm working on a special pathfinding system in java, which needs to print it's path at one point. It's far from done but I ran into a problem. When I run my code it instead prints a pointer towards an string rather then the string itself. Here is the code:
public class node {
int optionnum;
node[] options;
String[] dirrections;
String[] route;
boolean[] visited;
public node(){
options= new node[4];
dirrections= new String[4];
route= new String[50];
for (int i=0;i<50;i++){
route[i]="";
}
visited= new boolean[50];
}
public void revmdp(int num){
visited[num]=true;
for(int i=0;i<optionnum;i++){
System.out.println(options[i].route[0]); //how can this be a pointer?
options[i].revmdp(dirrections[i],num);
}
public void revmdp(String nroute, int num){
//System.out.println(route[0]+dirrections[0]);
if (!visited[num]||nroute.length()<route[num].length()){
route[num]=nroute;
visited[num]=true;
for(int i=0;i<optionnum;i++){
options[i].revmdp(route+dirrections[i],num);
}
}
}
}
output looks like this
[Ljava.lang.String;@2d66a22b3;
As you can see in the constructor of path I already set the path towards the string "" (empty string). As the string is not yet changed any futher at moment this code is called I would expect it to return "" however it instead gives these weird string pointers. Anybody know what's up?
Note I have already tried to call route[0][0] but java won't allow that.
解決
Update 3: I found it.
options[i].revmdp(route+dirrections[i],num);
Here you are doing string concatenation on an array and a String. This causes to set route[num]
in the level of recursion to this concat result.
他のヒント
Each Java class inerhits from the class Object
, which implements the default toString()
method. The Source code of the default toString() Method looks like :
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
If you do not override the default toString() method, the method above is called.