The arrays class of the first season of Java entry

Source: Internet
Author: User

The sorting problem is described earlier (see blog http://blog.csdn.net/qq_32059827/article/details/51362390); two-point lookup problems (see blog Http://blog.csdn.net/qq_ 32059827/article/details/51363949)


Next, a class is used to quickly implement sorting and two-point lookups. Arrays class


Import java.util.arrays;/* * Arrays: A tool class that operates on an array. such as sorting and finding. * 1:public static string toString (int[] a) returns a string representation of the contents of the specified array. * 2:public static void sort (int[] a) sorts the array, sorting the specified type of int in ascending order of numbers. * 3:public static int binarysearch (int[] a,int key) binary lookup. Use the binary search method to search for the specified array of type int to get the specified value.  * A-the array to search for key-the value to search for */public class Arraysdemo {public static void main (string[] args) {//define an array int[] arr = {24, };//public static string toString (int[] a) convert the array to a string System.out.println ("before sorting:" + arrays.tostring (arr));//P ublic static void sort (int[] a) sorts the array arrays.sort (arr);//The return value is a void type, sorted before output System.out.println ("After sorting:" + Arrays.tostring (arr));//[13, 24, 57, 69, 80]. Arr = {$, S, S, Min, 80}int type array//public static int BinarySearch (int[] a,int key) binary Find//NOTE: Binary lookup of an unordered expedited group index is an incorrect idea. System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 57)); System.out.println ("BinarySearch:" + arrays.binarysearch (arr, 558));}}

Visible sorting and binary lookups have become so simple.

The first season of Java 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.