Java 13-2 Arrays Tool class

Source: Internet
Author: User

1. Arrays: A tool class for manipulating arrays. such as sorting and finding.
1:public static string toString (int[] a) turns the array into a string
2:public static void sort (int[] a) sorting an array
3:public static int BinarySearch (int[] a,int key) Two-point lookup

1 ImportJava.util.Arrays;//search through API, does not belong to long package, so need guide package2  Public classArraysdemo {3  Public Static voidMain (string[] args) {4 //define an array5 int[] arr = {24, 69, 80, 57, 13 };6 7 //Public static string toString (int[] a) turns the array into a string8System.out.println ("Before sorting:" +arrays.tostring (arr));9 Ten //Public static void sort (int[] a) sorting an array One Arrays.sort (arr); ASystem.out.println ("After sorting:" +arrays.tostring (arr)); -  - //[13, 24, 57, 69, 80] to demonstrate the effect of the binary lookup method, the array is sorted first, not normally.  the //public static int BinarySearch (int[] a,int key) Two-point lookup -System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 57)); -System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 577)); - } +}

2, the Arrays tool class source code analysis
A, public static String toString (int[] a)
B, public static void sort (int[] a) The bottom layer is a quick sort, knowing it is OK.
C, public static int BinarySearch (int[] a,int key)

Development principles:
As long as it is an object, we need to determine whether the object is null.

Code to write:

1 int[] arr = {24, 69, 80, 57, 13 };2System.out.println ("Before sorting:" +arrays.tostring (arr));3 4 ToString Source code:5  Public StaticString toString (int[] a) {6 //A-arr--{ ------7 8 if(A = =NULL)9 return"NULL";//Description Array object does not existTen intIMax = a.length-1;//imax=4; One if(IMax = =-1) A return"[]";//the description array exists, but there are no elements.  -  -StringBuilder B =NewStringBuilder (); theB.append (' [');//"[" -  for(inti = 0;; i++) {//The middle is empty, then the default is true, Forever -B.append (A[i]);//"[ in the" - if(i = = IMax)//limit the number of cycles + //"[[A] , a. - returnB.append ('] '). toString (); +B.append (",");//"[The] A } at } ------------------------------------------------------ - code to write: - int[] arr = {13, 24, 57, 69, 80}; -System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 577)); -  in source code for BinarySearch: -  Public Static intBinarySearch (int[] A,intkey) { to //A-arr--{ ------- + //Key--577 - returnBinarySearch0 (A, 0, A.length, key); the } *  $ Private Static intBinarySearch0 (int[] A,intFromIndex,intToindex,Panax Notoginseng intkey) { - //A-arr--{ ------- the //FromIndex--0 + //Toindex--5 A //Key--577 the  +  - intlow = FromIndex;//Minimum index low=0 $ intHigh = toIndex-1;//Max Index high=4 $  -  while(Low <=High ) { - //unsigned right shift, equivalent to (Low+hith)/2 the intMid = (low + high) >>> 1;//mid=2,mid=3,mid=4 - intMidval = A[mid];//midval=57,midval=69,midval=80Wuyi  the if(Midval <key) -Low = mid + 1;//low=3,low=4,low=5 Wu Else if(Midval >key) -High = Mid-1; About Else $ returnMid//Key found - } - return-(low + 1);//key not found. -}

Java 13-2 Arrays Tool class

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.