2017-02-12 41 views
0

我需要複製一個2D布爾數組並將每個布爾值更改爲其相反的值......將true改爲false並將false改爲true。我意識到我的代碼中還可能存在其他一些問題,但這是我的主要問題。如何將2D數組中的每個布爾值更改爲相反?

我收到以下錯誤:

錯誤:不兼容的類型:布爾[]不能被轉換爲布爾[] []
布爾[] [] = newArray新的布爾[array.length];

錯誤:找不到符號
如果(newArray [I] [I] = FALSE)

我對每[I]變量相同的錯誤在if語句。

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; 

} 
} 
+0

我認爲錯誤信息已經足夠清楚了。你爲什麼不同意? –

+0

有一件事:你需要==來比較兩個布爾值。您在單個條件下的使用情況=條件是指派,而不是比較! – GhostCat

+0

我不知道如何解決這個錯誤。 – user3403708

回答

-1

我已經評論(並修改了)你的代碼,研究一下。

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

感謝您的建議。 – user3403708

2

只是遍歷數組,去

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

甚至更​​短:

array[row][column] ^= true; 

(使用XOR運算符爲「否定」每個單元格的當前內容在你的矩陣)

這就是你需要的全部toggle a bool值得!絕對不需要創建另一個數組。你那裏的所有代碼都可能消失!

所以真正的答案:學習布爾類型的所有基本知識......在查看數組的東西之前。含義:理解運營商喜歡的!不是所以你可以正確使用它們。

+0

或'array [x] [y]^= true',其優點是不會重複自己。 –

+0

@AndyTurner再次感謝;-) – GhostCat