2015-07-05 28 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其他一些地区,因为你是他们申报外循环。也许有些代码可以修改它们,但您并不知道副作用。