Reputation: 153
I am trying to move the false element in 2d array to a different blank space in element
'x''o'' '
' '' ''o'
'x''x''o'
the first element(x) in the array is false because it has no similar elements around it. i am trying to figure it out using nested for loop but not getting the result this is what i get
' ''o''x'
'x''x''o'
'x''x''o'
it moves that false x to all the blank spaces instead on just one blank space. Any tips please this is my code. I tried using break, but it does't work. tissue is the array passed in with the elements
char[][] arr = new char[tissue.length][tissue[0].length];
int count = 0;
char temp;
for(int i=0; i<tissue.length; i++){
for(int j=0; j<tissue[0].length; j++){
if(!isSatisfied(tissue,i,j,threshold)){
temp = tissue[i][j];
for(int k=0; k<tissue.length; k++){
for(int l=0; l<tissue.length; l++){
if(tissue[k][l] == ' '){
tissue[i][j] = arr[k][l];
arr[k][l] = temp;
}
}
}
}
}
}
Upvotes: 0
Views: 1129
Reputation: 56
First in
for(int l=0; l<tissue.length; l++){
It should be tissue[0].length ?
Second in
tissue[i][j] = arr[k][l];
arr should be initial before can be assigned to another variable.
Upvotes: 0
Reputation: 8849
You have to break the loop when it finds the first space for replace
if(!isSatisfied(tissue,i,j,threshold)){
temp = tissue[i][j];
outer:
for(int k=0; k<tissue.length; k++){
for(int l=0; l<tissue.length; l++){
if(tissue[k][l] == ' '){
tissue[i][j] = arr[k][l];
arr[k][l] = temp;
break outer;
}
}
}
}
Upvotes: 2