Search in two-dimensional array (Java version)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.