Java's Arrays Tool class combat

Source: Internet
Author: User

The Java.util.Arrays class makes it easy to manipulate an array, and all of the methods it provides are static. A static method belongs to a class, not to an object belonging to a class. So you can call directly using the class name plus the method name. arrays, as a tool class, can manipulate arrays well. Here are some of the main functions used.

  1.fill method

The Fill method is mainly used to populate the array, here we give the simplest type of int (other types of the same)

See Arrays the Fill source

  

Example code:

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. INTA[]=NEWINT[5];

    3. Fill fills an array

    4. Arrays.fill (a,1);

    5. for (inti=0;i<5;i++)//output 5 x 1

    6. System.out.println (A[i]);

    7. }

Fill part of the array source code:

  

Example:

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. INTA[]=NEWINT[5];

    3. Fill fills an array

    4. Arrays.fill (a,1,2,1);

    5. for (inti=0;i<5;i++)//a[1]=1, the rest defaults to 0

    6. System.out.println (A[i]);

    7. }

  2.sort method

From the method name, it is known that the array is sorted, still using the int type, other types.

Like a whole array sort, like

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. inta[]={2,4,1,3,7};

    3. Arrays.sort (a);

    4. for (inti=0;i<5;i++)//Ascending

    5. System.out.println (A[i]);

    6. }

To specify the ordering of array parts:

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. inta[]={2,4,1,3,7};

    3. Arrays.sort (a,1,4); Output 2,1,3,4,7

    4. for (inti=0;i<5;i++)

    5. System.out.println (A[i]);

    6. }

  3.equals method

Used to compare whether the element values in two arrays are equal or to see an array of type int. See Arrays Source

  

Example:

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. inta[]={2,4,1,3,7};

    3. inta1[]={2,4,1,5,7};

    4. System.out.println (Arrays.equals (A1, a)); Output false

    5. }

  4.BinarySearch Method

By using the BinarySearch method, the sorted array can be searched by binary method. See the source code as follows

  

Example:

Java code

    1. Publicstaticvoidmain (string[] args) {

    2. inta[]={2,4,1,3,7};

    3. Arrays.sort (a);//Sort First

    4. System.out.println (Arrays.binarysearch (A, 4));//binary lookup, Output 3

    5. }

  5.copyof method

Copying an array, arrays's copyof () method returns the array to the new array object, so you change the value of the element in the array, and it does not affect the original array

Such as:

Java code

    1. Importjava.util.Arrays;

    2. Publicclassarraydemo {

    3. Publicstaticvoidmain (string[] args) {

    4. Int[] arr1 = {1, 2, 3, 4, 5};

    5. int[] arr2 = arrays.copyof (arr1, arr1.length);

    6. for (inti = 0; i < arr2.length; i++)

    7. System.out.print (Arr2[i] + "");

    8. System.out.println ();

    9. }

    10. }

Java Arrays Tool class combat

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.