Java Api--arrays Class

Source: Internet
Author: User

1, Arrays class overview· A tool class that operates on an array. Provides functions such as sorting, finding and so on. 2. Member Methods· public static string ToString (Int[] a): in[] A can be changed to an array of other types, and the array is converted to a string · public static void sort (int[] a): Sorts arrays of various types in ascending order · public static int BinarySearch (int[] a,int key): Binary lookup of arrays of various types
 Public classArraysDemo01 { Public Static voidMain (string[] args) {//define an array        int[] arr = {24, 69, 80, 57, 13 }; //Public static string toString (int[] a) turns the array into a stringSystem.out.println ("Before sorting:" + arrays.tostring (arr));//before sorting: [ +, +, +--]//Public static void sort (int[] a) sorting an arrayArrays.sort (arr); System.out.println ("After sorting:" +arrays.tostring (arr));//after sorting: [[+]        int[] arr2 = {13, 24, 57, 69, 80}; //public static int BinarySearch (int[] a,int key) Two-point lookupSystem.out.println ("BinarySearch:" +arrays.binarysearch (arr2,57));//Binarysearch:2System.out.println ("BinarySearch:" +arrays.binarysearch (arr2,557));//binarysearch:-6 Return-(low+1)    }}

3. Arrays class Common method source detailed explanation

 Public StaticString toString (int[] a) Public Static voidSortint[] a) The bottom is a quick sort Public Static intBinarySearch (int[] A,intkey) Development principle: As long as the object, we must determine whether the object is null. Test code:int[] arr = {24, 69, 80, 57, 13 }; System.out.println ("Before sorting:" +arrays.tostring (arr)); Sort Source: Public StaticString toString (int[] a) {//A-arr--{ ------    if(A = =NULL)        return"NULL";//Description Array object does not exist    intIMax = a.length-1;//imax=4;    if(IMax = =-1)        return"[]";//the description array exists, but there are no elements. StringBuilder B =NewStringBuilder (); B.append (‘[‘);//"["     for(inti = 0;; i++) {b.append (a[i]);//"[ in the"        if(i = =IMax)//"[[A] , a.            returnB.append ('] '). toString (); B.append (", ");//"[The]    }}-----------------------------------------------------Test Code:int[] arr = {13, 24, 57, 69, 80}; System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 577) ); BinarySearch Source: Public Static intBinarySearch (int[] A,intkey) {    //A-arr--{ -------//Key--577    returnBinarySearch0 (A, 0, A.length, key);}Private Static intBinarySearch0 (int[] A,intFromIndex,intToindex,intkey) {    //A-arr--{ -------//FromIndex--0//Toindex--5//Key--577                                                                      intlow = FromIndex;//low=0    intHigh = toIndex-1;//high=4     while(Low <=High ) {        intMid = (low + high) >>> 1;//mid=2,mid=3,mid=4        intMidval = A[mid];//midval=57,midval=69,midval=80        if(Midval <key) Low= mid + 1;//low=3,low=4,low=5        Else if(Midval >key) high= Mid-1; Else            returnMid//Key found    }    return-(low + 1);//key not found.}

Java Api--arrays 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.