Java.util.Arrays class explanation

Source: Internet
Author: User

The Java.util.Arrays class includes a variety of static methods to implement array sorting and lookups, array comparisons, and arrays of filled elements. All of these methods have overloaded methods for all basic types.

You can use the Sort method to sort an entire array or a partial array. For example, the following code sorts a numeric array and a character array.

double[] numbers = {6.0,4.4,1.9,2.9,3.4,3.5};

Java.util.Arrays.sort (numbers); The final result is [1.9, 2.9, 3.4, 3.5, 4.4, 6.0] By default in ascending order

Char[] chars = {' A ', ' a ', ' 4 ', ' F ', ' D '};

Java.util.Arrays.sort (chars,1,3);

Call sort (numbers) to sort the entire array numbers. Call sort (chars,1,3) to sort the partial array from shars[1] to chars[3-1]

You can use the binary lookup method (BinarySearch method) to find the keyword in the array. The array must be ordered in advance, and if no keyword exists in the array, the method returns-(the insertion point subscript +). For example, the following code looks up the keyword in an array of integers and a character array: int[] list = {2,4,7,10,11,45,50,59,60,66,69,70,79};

System.out.println (Java.util.Arrays.binarySearch (list,11));

System.out.println (Java.util.Arrays.binarySearch (list,12));

The results obtained were 4,-6;

You can use the Equals method to detect whether two arrays are equal. If their contents are the same, name the two arrays are equal,

Int[] List1 = {2,3,7,10};

Int[] List2 = {2.3.7.10};

Int[] List3 = {3,2,7,10};

Java.util.Arrays.equals (LIST1,LIST2);//true

Java.util.Arrays.equals (LIST1,LIST3);//false

You can also use the Fill method, and I'm not going to say much.

Like Java programming Children's shoes, No.: Mjx_ java. Can pay attention to this platform, mainly to explain some good Java in the blog and information, in an update every day, we take a look at the car when the time to take a look at the car, can pass the time, but also to increase their Java knowledge, but also hope that we can communicate more in the platform, the increase in knowledge from the Exchange, If you have a good blog post or write your own article can also be sent to small, let the small part of the publication, will be labeled on the sender's name.

Java.util.Arrays class explanation

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.