Search in two-dimensional array (Java version)
Solution:
First, let's take the search for shuzi 7 as an example to analyze the search process step by step.
Then, we select 9 in the upper-right corner of the array.
Code implementation:
Package array; public class QuencyArray {public static boolean FindArray (int [] [] arr, int number) {int rows = arr. length; int columns = arr [0]. length; boolean flag = false; if (arr! = Null & rows> 0 & columns> 0) {int row = 0; int col = columns-1; while (row
0) {if (arr [row] [col]
Number) {// indicates that the number to be searched is in col --;} else {flag = true; break ;}} return flag ;} public static void main (String [] args) {int [] [] arr =, }, {,}; System. out. println (FindArray (arr, 8); // true System. out. println (FindArray (arr, 22); // false }}
For the first search, we select the upper right corner of the array for query. We can also select the lower left corner of the array.