Java Arrays Tool Class usage Java Arrays Tool class instance

Source: Internet
Author: User

1.fill Method

The Fill method is primarily used to populate arrays, and here we give the simplest type of int (the other types).

Look at arrays's fill source.

Sample code:

Java Code

Publicstaticvoidmain (string[] args) {

INTA[]=NEWINT[5];

Fill filled Array

Arrays.fill (a,1);

for (inti=0;i<5;i++)//Output 5 1

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

}

populate part of the array source code:

Example:

Java Code

Publicstaticvoidmain (string[] args) {

INTA[]=NEWINT[5];

Fill filled Array

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

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

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

}

2.sort Method

It is known from the method name that the array is sorted, still with the int type, and the other types are the same.

Same as having the entire array sorted, as

Java Code

Publicstaticvoidmain (string[] args) {

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

Arrays.sort (a);

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

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

}

Specify array partial sorting:

Java code

Publicstaticvoidmain (string[] args) {

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

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

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

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

}

3.equals Method

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

Example:

Java Code

Publicstaticvoidmain (string[] args) {

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

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

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

}

4.binarySearch Method

The BinarySearch method can be used to perform the binary search method for the sorted array. Look at the source code as follows

Example:

Java Code

Publicstaticvoidmain (string[] args) {

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

Arrays.sort (a);//First sort

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

}

5.copyof Method

To copy an array, the arrays copyof () method returns an array that is a new array object, so you change the value of the element in the returned array without affecting the original array

Such as:

Java Code

Importjava.util.Arrays;

Publicclassarraydemo {

Publicstaticvoidmain (string[] args) {

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

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

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

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

System.out.println ();

}

}

Related Article

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.