0

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;
                        }
                    }
                }
            }
        }
    }
2
  • what is this tissue? Could you post the definition of tissue i.e. delcaration and what value it has got? We want the tissue array that you are using. Commented Nov 14, 2014 at 21:08
  • tissue is the array passed in the method Commented Nov 14, 2014 at 21:08

2 Answers 2

2

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;
                            }
                        }
                    }
                }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. But what does outer: means/do
outer is not a command. it is just a label for break point. you can use any word in that label
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.