0

I'm trying to switch two elements in a 2d char array, and it's not working. I've read in other similar questions to this that the temp variable should be a 1d array, but I'm not convinced that's true. Can anyone help me understand why this isn't working?

public static void moveTo(char[][] tissue, int i, int j){
    char 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[k][l] = tissue[i][j];
                tissue[k][l] = temp;
                return;
            }
        }
    }
}
3
  • 3
    Why are you replacing ' ' twice with the character at tissue[i][j]? What is this supposed to do? Commented Nov 23, 2014 at 17:11
  • You probably need to give some more details here. There seem to be several flaws in your code. But what is actually wrong depends on what it should do. Commented Nov 23, 2014 at 17:14
  • temp and tissue[i][j] contain the same value. Therefore, no swap occurs inside the if() block. Commented Nov 23, 2014 at 17:33

3 Answers 3

3

In the second loop, you have to use tissue[k].length.

And tissue[i][j] must be affected with the blank character (if i am understanding well). temp is useless.

public static void moveTo(char[][] tissue, int i, int j){    
    for(int k = 0; k < tissue.length; k++){
        for(int l=0; l<tissue[k].length; l++){
            if(tissue[k][l] == ' '){
                tissue[k][l] = tissue[i][j];
                tissue[i][j] = ' ';
                return;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This makes so much sense. Thank you!
0

I think you're looking for this.

public static void moveTo(char[][] tissue, int i, int j){
    for(int k = 0; k < tissue.length; k++){
        for(int l=0; l<tissue.length; l++){
            if(tissue[k][l] == ' '){
                char temp = tissue[i][j];
                tissue[i][j] = tissue[k][l];
                tissue[k][l] = temp;
                return;
            }
        }
    }
}

That will search the 2D array for the first occurrence of ' ', and swap its content with the element at position i,j.

You're storing the temp because you will overwrite the value in tissue[i][j] soon, but need it afterwards to store in tissue[k][l]. In this case, you won't really need the temp since you know that tissue[k][l] is always ' '. So you might as well overwrite that first, without the need to store it, like in the other answer.

1 Comment

This is the same as @ToYonos answer, just using temp variable instead of the char literal.
0

This answer is based mostly on the title of the question. If you provide more information, we may be able to help a little bit more.

There are a few minor issues in the code that lead to it not working.

Firstly, any time you loop through multidimensional arrays, You'll want to make sure the inner loop uses the correct limit. tissue.length refers to the length of the outer array but there is no guarantee that each inner array in the 2d array (ie. tissue[0].length returns the length of an array and tissue[1].length may return a different value). So we'll first replace the inner loop limiter with tissue[k].length.

for(int k = 0; k < tissue.length ; k++){
    for(int l = 0 ; l < tissue[k].length ; l++){
        if(tissue[k][l] == ' '){
            tissue[k][l] = tissue[i][j];
            tissue[k][l] = temp;
            return;
        }
    }
}

Next issue I see is the swap. The code you have essentially looks for the first space in any array and inserts the value at i,j but you don't put anything into index i,j. what Elliot mentioned was that the two lines inside the if statement both replace tissue[k][l] with the same value, the one stored at tissue[i][j]. Instead, we'll change the first line to tissue[i][j] = tissue[k][l] to perform the swap. So we end up with the follow method which swaps the first space character in the first string it finds with the character at i,j.

public static void moveTo(char[][] tissue, int i, int j){
    char temp = tissue[i][j];

    for(int k = 0; k < tissue.length ; k++){
        for(int l = 0 ; l < tissue[k].length ; l++){
            if(tissue[k][l] == ' '){
                tissue[i][j] = tissue[k][l];
                tissue[k][l] = temp;
                return;
            }
        }
    }
}

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.