java - How would I modify my code to search a multidimensional array diagonally from left to right and right to left? -
java - How would I modify my code to search a multidimensional array diagonally from left to right and right to left? -
i have code searching multidimensional array left right , right left.
what need search array top left bottom right,the top right bottom left, bottom left top right, , bottom right top left.
what need alter in existing methods create new method want?
// left right public static string findlefttoright (char[][]board, string word) { char[] letters = word.tochararray(); (int = 0; < board.length; i++){ (int j = 0; j < board[i].length; j++) { boolean found = true; (int k = 0; k < letters.length; k++) { if ((j+k >= board[i].length) || (letters[k] != board[i][j+k])) { found = false; break; } } if (found) { homecoming "string " + word + " found in row=" + + " col=" +j; } } } homecoming "string " + word + " not found"; } // end findlefttoright // right left public static string findrighttoleft (char[][]board, string word) { char[] letters = word.tochararray(); (int = board.length-1; > -1; i--){ (int j = board[i].length-1; j > -1; j--) { boolean found = true; (int k = 0; k < letters.length; k++) { if ((j - k < 0) || (letters[k] != board[i][j-k])) { found = false; break; } } if (found) { homecoming "string " + word + " found in row=" + + " col=" +j; } } } homecoming "string " + word + " not found"; } // end findlefttoright
so assume multidimensional array cube means row , column has same length.
from top left bot right compare want board[i][i];
for top right bot left same loop compare board[length-i][i]
for bot left top right compare board[i][length-i]
for bot right top left compare board[length-i][length-i]
all of them in loop length
java arrays search matrix multidimensional-array
Comments
Post a Comment