Java Common Class (iv) Array tool class arrays

Source: Internet
Author: User

Objective

Array of tool classes Java.util.Arrays
Since there is no way for us to invoke the array object itself, the API provides a tool class arrays for us to use, allowing you to perform some basic operations on the data object.

I. Arrays class overview 1.1, Introduction of arrays Class

This is the class in the Java.util package, which we would like to use in our code to import.

In the current Class A, only the classes under the Java.lang package, and the classes under the same package as the current Class A, do not require import , and all other classes under the package are imported before they are used.

1.2. Introduction

  

Second, the method call in the Arrays class

The methods in the arrays class are static methods that are statically modified , and can be called directly using the class name when used, instead of using the object to invoke (note: "No" instead of " no")

2.1. ToString () method

 Converts an array to a bit string and returns

  

        int [] A = {1,3,5,7,9};        System. out. println (Arrays.tostring (a));   

Results: [1, 3, 5, 7, 9]

2.2. BinarySearch () method

  Finds the specified element in the array and returns its subscript

Note: Use the binary search method to search for the specified array to get the specified value. You must sort the array before making this call (by using the Sort method, and so on). If no array is ordered, the result is indeterminate.

If the array contains more than one element with the specified value, it is not guaranteed which one is found.

  

        int [] A = {1,3,5,7,9};         int 7 );        System. out. println (BS);  

Results: 3

2.3. Sort () method

  Sort the elements in the data

  

1) sort(int[] a) sort the specified array of type int in ascending order of numbers

        int [] A = {3,5,1,9,7};        System. out. println ("before"+arrays.tostring (a));        Arrays.sort (a);        System. out. println ("after"+arrays.tostring (a));    

Results: Before[3, 5, 1, 9, 7]
After[1, 3, 5, 7, 9]

2) The specified sort(byte[] a, int fromIndex, int toIndex) range of specified byte arrays is sorted in ascending order of numbers.

Overview of parameters:

a-the array to sort

      fromIndex-index of the first element to sort (including)

      toIndex-index of the last element to sort (not included)

3)sort (t[] A, comparator<? super t> C) sort the specified array of objects according to the order produced by the specified comparer

2.4. CopyOf () method

  Copies or intercepts the specified array and returns

  

        int [] A = {1,3,5,7,9};         int 8 );        System. out. println (arrays.tostring (copy));  

Results: [1, 3, 5, 7, 9, 0, 0, 0]

2.5. Copyofrange () method

  Copies the specified range in the array to a new array and returns

Copies the specified copyOfRange(int[] original, int from, int to) range of the specified array to a new array.

Parameter: original -the array from which a range will be copied

      from-Initial index of the range to be copied (included)

      to-The last index (not included) of the range to copy. (This index can be outside the array range.)

  

2.6. Equals () method

  Compare two arrays for equality

  

        int [] A = {1,3,5,7,9};         int [] B = {1,3,5,7,9};         = Arrays.equals (A, b);        System.  out . println (eq);        System. out. println (a==b);

Result: True

False

Note: = = compares the memory address of the object to which the reference is directed
Arrays.equals method Comparison is the content of the two arrays

2.7. Fill () method

  Fills the array object with the specified value

  

1)fill(int[] a, int val)    将指定的 int 值分配给指定 int 型数组的每个元素

        int [] A = {1,3,5,7,9};         666 );        System. out. println (Arrays.tostring (a));

Results: [666, 666, 666, 666, 666]

2) fill(int[] a, int fromIndex, int toIndex, int val) assigns the specified int value to each element in the specified range in the specified array of type int .

Parameters: a -the array to populate

        fromIndex-Index of the first element to be populated with the specified value (including)

        toIndex-Index of the last element to be populated with the specified value (not included)

        val-Values to store in all elements of the array

2.8. Aslist () method

  You can convert an array to a list collection

  

        int [] A = {3,5,1,9,7};        List<int[]> list = Arrays.aslist (a);
2.9. Hashcode () method

  Returns a hash code based on the contents of the specified array

  

        int [] A = {3,5,1,9,7};        System. out. println (Arrays.hashcode (a));  

Results: 31549916

Like on the point of a "recommendation" Oh!

Java Common Class (iv) Array tool class arrays

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.