Find in a two-dimensional array (Java edition)

Source: Internet
Author: User

Problem Solving Ideas:
First, we choose to find the number of sub 7 as an example to step through the process of finding.
We then select 9 in the upper-right corner of the array.

Code implementation:

Package array; Public classQuencyarray { Public StaticBooleanFindarray(int[] arr,intNumber) {introws = arr.length;intcolumns = arr[0].length; Boolean flag =false;if(arr!=NULL&& rows>0&& columns>0){introw =0;intCol = columns-1; while(Row<rows && col>0){if(Arr[row][col]<number) {//Description of the number to be searched belowrow++; }Else if(Arr[row][col]>number) {//Description of the number to be found on the leftcol--; }Else{flag =true; Break; }            }        }returnFlag } Public Static void Main(string[] args) {int[] arr = {{1,2,8,9},            {2,4,9, A},            {4,7,Ten, -},            {6,8, One, the},            {8,Ten, -, -}        }; System. out. println (arr, Findarray8));//trueSystem. out. println (arr, Findarray A));//false}}

For the first time, we select the upper-right corner of the array to query, and we can select the lower-left corner of the array.

Find in a two-dimensional array (Java edition)

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.