2015-07-05 27 views
2

當我在Android設備上運行下面的代碼時,我得到一個ArrayOutOfBounds異常說y = 6。但是,理論上這是不可能的,因爲for循環的邊界是y < 6.我搜索了可能的解釋,發現沒有。 matrix1Numbers的大小是6乘6,每個維度保持0-5。Java Android Studio For循環超出其極限

for (z = 0; z < 6; ++z) { 
     for (y = 0; y < 6; ++y) { 
      matrix1Numbers[z][y].addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged( CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        if (matrix1Numbers[z][y].getText().toString().equals("")) { 
         for (int a = 0; a < z; ++a) { 
          for (int b = y; b < 6; ++b) { 
           matrix1Numbers[a][b].setText(""); 
           matrix1Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 

         for (int a = z; a < 6; ++a) { 
          for (int b = 0; b < y; ++b) { 
           matrix1Numbers[a][b].setText(""); 
           matrix1Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 

         for (int a = z; a < 6; ++a) { 
          for (int b = y; b < 6; ++b) { 
           matrix1Numbers[a][b].setText(""); 
           matrix1Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 
        } else { 
         if (z > matrix1MaxX) { 
          matrix1MaxX = z; 
         } 
         if (y > matrix1MaxY) { 
          matrix1MaxY = y; 
         } 

         for (int a = 0; a < matrix1MaxX; ++a) { 
          for (int b = 0; b < matrix1MaxY; ++b) { 
           matrix1Numbers[a][b].setBackgroundColor(Color.BLUE); 
          } 
         } 
        } 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 


      matrix2Numbers[z][y].addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

       } 

       @Override 
       public void onTextChanged(CharSequence s, int start, int before, int count) { 
        if (matrix2Numbers[z][y].getText().toString().equals("")) { 
         for (int a = 0; a < z; ++a) { 
          for (int b = y; b < 6; ++b) { 
           matrix2Numbers[a][b].setText(""); 
           matrix2Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 

         for (int a = z; a < 6; ++a) { 
          for (int b = 0; b < y; ++b) { 
           matrix2Numbers[a][b].setText(""); 
           matrix2Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 

         for (int a = z; a < 6; ++a) { 
          for (int b = y; b < 6; ++b) { 
           matrix2Numbers[a][b].setText(""); 
           matrix2Numbers[a][b].setBackgroundColor(Color.WHITE); 
          } 
         } 
        } else { 
         if (z > matrix2MaxX) { 
          matrix2MaxX = z; 
         } 
         if (y > matrix2MaxY) { 
          matrix2MaxY = y; 
         } 

         for (int a = 0; a < matrix2MaxX; ++a) { 
          for (int b = 0; b < matrix2MaxY; ++b) { 
           matrix2Numbers[a][b].setBackgroundColor(Color.BLUE); 
          } 
         } 
        } 
       } 

       @Override 
       public void afterTextChanged(Editable s) { 

       } 
      }); 



     } 
+0

'y'和'z'如何存儲?我的意思是..是靜態還是其他? – Rothens

+0

@ Rothens,你說得對,我檢查並刪除了我的評論 –

+0

@darkterbears,請告訴我們'Exception'發生在哪裏(在哪一行) –

回答

1

檢查代碼的工作與你的yz其他一些地區,因爲你是他們申報外循環。也許有些代碼可以修改它們,但您並不知道副作用。