I have a code in which I am using replace method on Java String, but it is not working.. Please point out the mistake..
static String[] cavityMap(String[] grid) {
String[] ans = grid;
for(int i = 1; i<= grid.length-2; i++){
for(int j = 1; j<= grid[i].length()-2; j++){
int e = Integer.parseInt(grid[i].charAt(j) + "");
int t = Integer.parseInt(grid[i - 1].charAt(j) + "");
int b = Integer.parseInt(grid[i + 1].charAt(j) + "");
int l = Integer.parseInt(grid[i].charAt(j - 1) + "");
int r = Integer.parseInt(grid[i].charAt(j + 1) + "");
if(e > t && e > b && e > l && e > r){
ans[i] = ans[i].replace("X",(ans[i].charAt(j) + ""));
System.out.println(ans[i].replace("X",(ans[i].charAt(j) + "")));
}
}
}
return ans;
}
The code execution is going in if conditional part.. But it is printing the same value as before.. Why is it not replacing the String with "X".. Thanks in advance
ans[i].charAt(j)andans[i]