简体   繁体   English

如何将2D数组中的每个布尔值更改为相反的值?

[英]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. 我需要复制一个2D布尔数组并将每个布尔值更改为相反的值...将true更改为false,将false更改为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 []无法转换为boolean [] []
boolean[][] newArray = new boolean[array.length]; boolean [] [] newArray =新的boolean [array.length];

error: cannot find symbol 错误:找不到符号
if(newArray[i][i] = false) if(newArray [i] [i] =假)

I have the same error for every [i] variable in the if statements. 对于if语句中的每个[i]变量,我都有相同的错误。

    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) (使用XOR运算符“取反”矩阵中每个单元格的当前内容)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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