简体   繁体   中英

How can I change every boolean value in a 2D array to its opposite?

I need to copy a 2D boolean array and change every boolean value to its opposite... Change true to false and false to true. I realize I proably have some other problems in my code as well, but that is my primary problem.

I am receiving the following errors:

error: incompatible types: boolean[] cannot be converted to boolean[][]
boolean[][] newArray = new boolean[array.length];

error: cannot find symbol
if(newArray[i][i] = false)

I have the same error for every [i] variable in the if statements.

    public class mod4Lec
{
public static void main(String[] args) {

//Creates array of boolean
boolean[][] array  = {
{true, false, true, false},
{false, true, false, true},
{true, false, true, false},
{false, true, false, true},
};

System.out.println("Before: ");
//Prints original array

for(int row=0; row<array.length; row++) {
   for(int column=0; column<array[row].length; column++)
       System.out.print(array[row][column] + "  ");
   System.out.println();
   }
}

//Pass array to method
public static void swapArray(boolean[][] array){

//Copy array


 boolean[][] newArray = new boolean[array.length];
 for (int column = 0; column < array.length; column++)
 newArray[row][column] = array[row][column];

 //Search for boolean true and switch to false
 for (int i = 1; i < newArray.length; i++){
 if(newArray[i][i] = true)
 newArray[i][i] = false;
  }
 //Search for boolean false and switch to true
 if(newArray[i][i] = false){
  newArray[i][i] = true;
 }
   return newArray;

 }
}

Just iterate the array and go for

array[row][column] = ! array[row][column];

or even shorter:

array[row][column] ^= true;

(using the XOR operator to "negate" the current content of each cell in your matrix)

That is all you need to toggle a boolean value! There is absolutely no need to create another array. All of your code there could go away!

So the real answer: learn all the basics of the boolean type... Before looking into the array stuff. Meaning: understand the operators like ! not so you can make proper use of them.

I've commented (and modified) your code a little bit, study it.

public class Mod4Lec { //class names are started from Uppercase letter
    public static void main(String[] args) {

        // Creates array of boolean
        boolean[][] array = { { true, false, true, false }, { false, true, false, true }, { true, false, true, false },
                { false, true, false, true }, };

        System.out.println("Before: ");
        // Prints original array

        for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++)
                System.out.print(array[row][column] + "  ");
            System.out.println();
        }

        boolean[][] newArray = swapArray(array); 
        System.out.println("After: ");
        for (int row = 0; row < newArray.length; row++) {
            for (int column = 0; column < newArray[row].length; column++)
                System.out.print(newArray[row][column] + "  ");
            System.out.println();
        }

    }

    // Pass array to method
    public static boolean[][] swapArray(boolean[][] array) { //return 2d array, not void
        // Copy array
        // you should have 2d array here, my guess is that every array "row" is of the same size
        boolean[][] newArray = new boolean[array.length][array[0].length];  
        // again, we have 2d array and need to go for each rows to each columns
        for (int row = 0; row < array.length; row++) {
            for (int column = 0; column < array[row].length; column++)
                newArray[row][column] = array[row][column];
        }
        // Search for boolean true and switch to false
        // the same 2d
        for (int i = 0; i < newArray.length; i++) {
            for (int j = 0; j < newArray[i].length; j++) //2nd dimension
                newArray[i][j] = !newArray[i][j]; // just invert 
        }
        return newArray;
    }
}

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