简体   繁体   中英

Java switching char elements in a 2d array

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;
            }
        }
    }
}

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;
            }
        }
    }
}

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.

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;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM