2013-02-21 30 views
1

我在這裏有4x4字符數組,我需要獲得數組邊緣的字符的公共值......我嘗試瞭解決方案類似我的問題其他問題,但我仍然得到同樣的錯誤,獲取二維數組的邊緣值,同時防止越界

這裏是我的代碼..

//arr2[][] 
//  arr2[3][0] = 'H'; 
//  arr2[3][1] = 'E'; 
//  arr2[3][2] = 'L'; 
//  arr2[3][3] = 'P'; 
//arr3[][] 
//  arr3[1][3] = 'T'; 
//  arr3[2][3] = 'O'; 
//  arr3[3][3] = 'P'; 
//I specifically need the get the 'P' at [3][3].. 
for(o = 0;o<count;o++){ 
     char letter = out.charAt(o);       
     for(int m = 0; m < 4; m ++){  
      for(int n = 0; n < 4; n ++){ 
       if(Arrays.asList(arr3[m][n]).contains(letter)){ 
        r = m; 
        c = n; 
       } 
      } 
     } 
     right = arr2[r][c+1]; 
     left = arr2[r][c-1]; 
     up = arr2[r-1][c]; 
     down = arr2[r+1][c]; 
     if(o==0){ 
       if(c==0){ 
        if(r==0||r==3){ 
         if(right!=null){ 
          l = right; 
         } 
        }else{ 
         if(right!=null){ 
          l = right; 
         }else if(up!=null){ 
          l = up; 
         } 
        } 
       }else if(c==3){ 
        if(r==0||r==3){ 
         if(left!=null){ 
          l = left; 
         } 
        }else{ 
         if(left!=null){ 
          l = left; 
         }else if(up!=null){ 
          l = up; 
         } 
        } 
       }else{ 
        if(r==0||r==3){ 
         if(left!=null){ 
          l = left; 
         }else if(right!=null){ 
          l = right; 
         } 
        }else{ 
         if(left!=null){ 
          l = left; 
         }else if(right!=null){ 
          l = right; 
         }else if(up!=null){ 
          l = up; 
         } 
        } 
       } 
      } 
     }else if(o==(count-1)){ 
      if(vertical == 1){ 
       if(c==0){ 
        if(r==0||r==3){ 
         if(right!=null){ 
          l = right; 
         } 
        }else{ 
         if(right!=null){ 
          l = right; 
         }else if(down!=null){ 
          l = down; 
         } 
        } 
       }else if(c==3){ 
        if(r==0||r==3){ 
         if(left!=null){ 
          l = left; 
         } 
        }else{ 
         if(left!=null){ 
          l = left; 
         }else if(down!=null){ 
          l = down; 
         } 
        } 
       }else{ 
        if(r==0||r==3){ 
         if(left!=null){ 
          l = left; 
         }else if(right!=null){ 
          l = right; 
         } 
        }else{ 
         if(left!=null){ 
          l = left; 
         }else if(right!=null){ 
          l = right; 
         }else if(down!=null){ 
          l = down; 
         } 
        } 
       } 
      } 
     }else{ 
      if(vertical == 1){ 
       if(c==0){ 
        if(right!=null){ 
         l = right; 
        } 
       }else if(c==3){         
        if(left!=null){ 
         l = left; 
        } 
       }else{ 
        if(right!=null){ 
         l = right; 
        }else if(left!=null){ 
         l = left; 
        } 
       } 
      } 
     } 
     k = Character.toString(letter); 
     letr = Character.toString(l); 
+0

爲什麼你寫'if(Arrays.asList(arr3 [m] [n])。contains(letter)){'而不是'if(arr3 [m] [n] == letter){'?? – jlordo 2013-02-21 14:45:45

+0

@jlordo我在這個之上有其他程序,我創建了一個字符串從arr3這是TOP的值..我想知道每個字符的位置和做的方法是檢查它是否包含在arr3 [m] [n] ... – kathy 2013-02-21 14:51:48

+0

我想你沒有得到任何支持,因爲你的代碼太複雜了。許多'if','else if'和'else'的情況,帶有單個字母的變量名稱。你說'//我特別需要在[3] [3]處得到'P'..'這可以通過單線'arr3 [3] [3]'完成。我不明白你的代碼,既不是你的問題。至於我以前的評論,兩個條件是相同的,而我的閱讀理解(更高效)容易,而你的過於複雜。 – jlordo 2013-02-21 14:56:45

回答

0

可以運行此,讓我們知道,如果有幫助?

char[][] arr2 = ... // your array 
char[][] arr3 = ... // your other array 
// inspect all common indeces: 
for (int i = 0; i < arr2.length && i < arr3.length; i++) { 
    for (int j = 0; j < arr2[i].length && j < arr3[i].length; j++) { 
     // print value, if value is the same: 
     if (arr2[i][j] == arr3[i][j]) { 
      // here you know that two characters on the same index are the same. 
      // you have the information about the character and the indeces: 
      System.out.println("Found common value at index (i,j)->" 
        + "(" + i + "," + j + "), char '" + arr2[i][j] + "'"); 
     } 
    } 
} 

使用像在您的評論例如數組,這提供了以下的輸出:

Found common value at index (i,j)->(3,3), char 'P'

這似乎是你在找什麼。假設兩個數組具有相同的維度,則此解決方案會查看兩個數組中的每個索引。如果一個數組比另一個數組大,那麼只有通用索引被查看。你提到了有關數組邊緣的內容。我不清楚,如果你的意思是只有邊緣包括邊緣。這個答案包括邊緣。如果您只需要查看邊緣,請在答案下方留言。

+0

在我看來,OP只想要在陣列的*邊*上的常見值......但它仍然需要她的確認。 – sp00m 2013-02-21 15:35:12

+0

@ sp00m:如果OP確認我可以很容易地適應我的答案;) – jlordo 2013-02-21 15:35:49

+0

@jlordo我得到了P.thanks..This更容易 – kathy 2013-02-21 15:42:20

0

在你的代碼,

for(int m = 0; m < 4; m ++){  
    for(int n = 0; n < 4; n ++){ 
     if(Arrays.asList(arr3[m][n]).contains(letter)){ 
      r = m; 
      c = n; 
     } 
    } 
} 

這裏給您分配R和C這很可能是在某些情況下03,然後你的下面的代碼,

right = arr2[r][c+1]; 
left = arr2[r][c-1]; 
up = arr2[r-1][c]; 
down = arr2[r+1][c]; 

在上面的任務,你有c + 1,r + 1,c - 1,r - 1如果前面的循環將r和c分配給0或3,肯定會超出範圍。

+0

Madamsamy是的,這就是爲什麼我有很多if else語句來防止出界。, – kathy 2013-02-21 15:45:15

+0

上述分配甚至在那些if語句之前。 – 2013-02-21 15:46:17

+0

所以我需要把這些陳述內的if語句?..我堅持認爲把它放在那裏,以便我不需要重複聲明它..感謝您的幫助 – kathy 2013-02-21 15:48:16

0

感謝那些誰幫我弄清楚這一個.. 我能夠同時使用modulo..I能夠防止走出去界的一個陣列的邊緣,以獲取值... https://stackoverflow.com/a/12743834/2078012 這從另一個問題的鏈接回答我的問題.. 再次謝謝.. :-)