I'm currently working on a pathfinding algorithm called: Jump Point Search.
To check nodes I made an 2D Node array of nodes with a class "Node" (landSeaData
). When I try to find a path I fill another global 2D Node array (tempWorld
) with ".clone()" to make sure tempWorld
isn't a pointer of landSeaData
:
tempWorld = landSeaData.clone();
This clone method returns a new 2D Node array, but the indices are still pointer of the cloned array. I checked is with println:
System.out.println(tempWorld[1][0] +" , "+ landSeaData[1][0]);
Output:
JPS.Node@4cc77c2e , JPS.Node@4cc77c2e
How do I prevent this or do I have to copy it another way, to make sure the indices are not pointers of each other?
EDIT
I've created a deep copier for the 2D array, but it takes to long, 47 milliseconds:
tempWorld = new Node[landSeaData.length][landSeaData[0].length];
for(int x = 0; x < landSeaData.length; x++) {
for(int y = 0; y < landSeaData[0].length; y++) {
if(landSeaData[x][y] != null) {
tempWorld[x][y] = new Node(landSeaData[x][y].position);
}
}
}
Is there a faster way to deep copy?