Java Array application and Method

Source: Internet
Author: User

1. array Inversion

CopyCode The Code is as follows: Import java. util. arrays;

Public class arrayreverse {
Public static void main (string [] ARGs ){
Int [] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
Reverse (ARR );
}
Public static void reverse (INT [] ARR ){
For (INT I = 0; I <arr. Length/2; I ++ ){
Int temp = arr [I];
Arr [I] = arr [arr. length-1-i];
Arr [arr. length-1-i] = temp;
}
System. Out. println (arrays. tostring (ARR ));
}
}

2. Evaluate the most value
Copy codeThe Code is as follows: Class zuizhi
{
Public static void main (string [] ARGs)
{
Int [] A = {2, 92, 34, 54, 28 };
Minmethod ();
Maxmethod ();
}
Public static void minmethod (INT [])
{
Int min = A [0]; // The array cannot be initialized as 0 and may have negative numbers.
For (INT I = 0; I <A. length; I ++)
{
If (A [I] <min)
Min = A [I];
}
System. Out. println ("the smallest element in the array is:" + min );
}
Public static void maxmethod (INT [])
{
Int max = A [0]; // The array cannot be initialized as 0 and may have negative numbers.
For (INT I = 0; I <A. length; I ++)
{
If (A [I]> MAX)
Max = A [I];
}
System. Out. println ("the largest element in the array is:" + max );
}

}

3. half-lookup
copy Code the code is as follows:/*
half-lookup:
3 5 7 9 10 14
min = 0 max = length-1 MID = (MAX + min) /2
*/
class binarysearch
{< br> Public static void main (string [] ARGs)
{< br> int [] A = {3, 5, 7, 9, 10, 14 };< br> int Index = binarysearch_1 (A, 7);
system. out. println (INDEX);
Index = binarysearch_2 (A, 14);
System. out. println (INDEX);
}< br> Public static int binarysearch_1 (INT [] A, int key)
{< br> int min = 0, mid, max =. length-1;
mid = (min + max)/2;
while (key! = A [Mid])
{< br> if (a [Mid]> key)
{< br> max = mid-1;
}< br> else if (a [Mid]
{< br> min = Mid + 1;
}< br> If (max
return-1;
mid = (min + max)/2;

}
Return mid;
}
Public static int binarysearch_2 (INT [] A, int key)
{
Int min = 0, max = A. Length-1, mid;
While (Min <= max)
{
Mid = (MAX + min)> 1;
If (A [Mid]> key)
{
Max = mid-1;
}
Else if (a [Mid] <key)
{
Min = Mid + 1;
}
Else
Return mid;
}
Return-1;
}

}

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.