Arrays classes of Java arrays and their methods

Source: Internet
Author: User

public class Arraysextends object This class contains various methods for manipulating arrays, such as sorting and searching. This class also contains a static factory that allows the array to be viewed as a list.

Unless otherwise noted, the methods in this class will throw NullPointerException if the specified array reference is NULL.

The documentation for the methods contained in this class includes a short description of the implementation. These descriptions should be considered as implementation considerations and should not be considered part of the specification. The implementation should be free to replace other algorithms as long as they follow the specification itself. (for example, the algorithm used by sort (object[]) does not have to be a merge sort algorithm, but it must be stable. )

This class is a member of the Java collections Framework.

1 Importjava.util.Arrays;2  Public classTestarrays {3      Public Static voidMain (string[] args) {4         int[] A =New int[]{11,31,24,98,-9,56,73,-27};5         int[] B =New int[]{11,31,24,98,-9,56,73,-27};6         7         //static void sort (int[] a) sorts the specified array of type int in ascending order of numbers. 8 Arrays.sort (a);9          for(inti = 0; i < a.length; i++){TenSystem.out.print (A[i] + "\ T"); One         } A System.out.println (); - System.out.println (); -          the         //The static int BinarySearch (int[] A, int key) uses the binary search method to search for the specified array of type int to obtain the specified value.  -         inttemp = Arrays.binarysearch (A, 73); - System.out.println (temp); - System.out.println (); +          -         //Static int[] CopyOf (int[] original, int newlength) +         //copies the specified array, intercepts, or fills with 0 (if necessary) so that the copy has the specified length.  A         int[] t =arrays.copyof (A, a.length); at          for(inti = 0; i < a.length; i++){ -System.out.print (T[i] + "\ T"); -         } - System.out.println (); - System.out.println (); -          in         //static Boolean equals (Int[] A, int[] A2) returns true if the two specified array of type int is equal to each other.  -         BooleanBool1 =Arrays.equals (A, t); to System.out.println (bool1); + System.out.println (); -         BooleanBool2 =Arrays.equals (A, b); the System.out.println (bool2); * System.out.println (); $         Panax Notoginseng         //static int hashcode (int[] a) returns a hash value based on the contents of the specified array.  -         intS1 =Arrays.hashcode (a); the         intS2 =Arrays.hashcode (b); +         intS3 =Arrays.hashcode (t); A System.out.println (S1); the System.out.println (S2); + System.out.println (S3); - System.out.println (); $          $         //the static string toString (int[] a) returns a string representation of the contents of the specified array.  -String str =arrays.tostring (a); - System.out.println (str); the     } -}

The results of the program run as follows:

-27    -9    -    98    6-27    -9    73    98    true false-678226926-1591174308-678226926[-27,-9, 11, 24, 31, 56, 73, 98]

Note: Theequals method of the Arrays class compares the hashcode value of two arrays. If the two hashcode values are the same, the two arrays are the same array and return true, otherwise, false is returned.

    if (A.hashcode = = B.hashcode        ) return true ;     Else        return false;

Arrays classes of Java arrays and their methods

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.