Java thousand ask _06 data structure (019) _arrays class have what function

Source: Internet
Author: User

Click to enter _ more _java thousand ask

1. What is the function of arrays class?

The classes in Java.util.Arrays contain a number of static methods for sorting arrays, searching arrays, comparing arrays, and populating array elements.

Understanding a one-dimensional array look here: What is an array in Java

The common methods are as follows:

    1. public static int BinarySearch (object[] A, Object key)
      Searches for the position (subscript) of the specified value in the array using the dichotomy method. If the array type is not a basic data type, the class is required to implement the CompareTo method in the comparable interface.

    2. public static Boolean Equals (Long[] A, long[] A2)
      Compares two arrays for equality. Returns true if two of the specified arrays are equal. The default method for determining the equality of two arrays is:

      • Two arrays contain the same number of elements, and all elements of the two array correspond to equal.
      • The same method can be used for all other data types (byte, short, int, etc.)
    3. public static void Fill (int[] A, int val)
      Fills all the elements of the array with the specified int value. The same method can be used for all other raw data types (byte, short, int, and so on).
      There is also a method for specifying the fill location: public static void Fill (byte[] A, int fromIndex, int toindex, byte val), you can specify some elements to populate.

4.public static String toString (object[] a)
The values of each element are assembled sequentially into a string of type strings, and the same method can be used for all other data types (byte, short, int, etc.)

    1. public static void sort (object[] a)
      The specified array is sorted in ascending order according to the natural ordering of its elements, and the same method can be used for all other data types (byte, short, int, etc.)
2. How to use arrays

The following examples show: BinarySearch, CopyOf, Copyofrange, equals, fill, sort, tostring and other methods. As follows:

Import Java.util.Arrays; Public classtestarrays { Public Static void Main(string[] args) {int[] A = {3,4,5,6};int[] B = {3,4,5,6}; System. out. println ("A and B are equal:"+ Arrays.equals (A, b));//trueSystem. out. println ("5 position in a:"+ Arrays.binarysearch (A,5));//2        int[] C = arrays.copyof (A,6); System. out. println ("A and C are equal:"+ Arrays.equals (A, c));//falseSystem. out. println ("element of C:"+ arrays.tostring (c));//3,4,5,6,0,0Arrays.fill (c,2,4,1);//assigns the 3rd to 5th Element (not included) in C to a value of 1System. out. println ("element of C:"+ arrays.tostring (c));//3,4,1,1,0,0Arrays.sort (c); System. out. println ("element of C:"+ arrays.tostring (c));//0,0,1,1,3,4}}

The results of the implementation are as follows:
A and B are equal: true
5 position in a: 2
A and C are equal: false
Elements of C: [3, 4, 5, 6, 0, 0]
Elements of C: [3, 4, 1, 1, 0, 0]
Elements of C: [0, 0, 1, 1, 3, 4]

Java thousand ask _06 data structure (019) _arrays class have what function

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.