代码实现:
package j2; /** * 二维数组中的查找 * Created by admin on 2019/5/15. */ public class ArrayFind { public static boolean find(int [][]matrix,int rows,int cols,int key){ boolean found = false; //边界判断 if (matrix != null && rows>=0 && cols>=0) { int row = 0; int col = cols-1; while (row < rows && col>0){ if (matrix[row][col] == key) { found = true; break; } else if(matrix[row][col] > key){ col--; } else { row++; } } } return found; } public static void main(String[] args) { int[][]ma = { {1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,15} }; boolean result = find(ma,4,4,5); System.out.println(result); } }