Home  >  Article  >  Java  >  Practical combat of java's Arrays tool class

Practical combat of java's Arrays tool class

高洛峰
高洛峰Original
2017-01-11 14:46:131245browse

The java.util.Arrays class can conveniently operate arrays, and all methods it provides are static. Static methods belong to classes, not objects of classes. So you can directly use the class name plus the method name to call it. Arrays, as a tool class, can operate arrays very well. The following introduces several functions that are mainly used.

1.fill method

The fill method is mainly used to fill arrays. Here we take the simplest int type (the same as other types)

Look at the fill source code of Arrays

Practical combat of javas Arrays tool class

Example code:

Java code

publicstaticvoidmain(String[] args) {
inta[]=newint[5];
//fill填充数组
Arrays.fill(a,1);
for(inti=0;i<5;i++)//输出5个1
System.out.println(a[i]);
}

Fill part of the array source code:

Example:

Java code

publicstaticvoidmain(String[] args) {
inta[]=newint[5];
//fill填充数组
Arrays.fill(a,1,2,1);
for(inti=0;i<5;i++)//a[1]=1,其余默认为0
System.out.println(a[i]);
}

2.sort method

You can tell from the method name that it is used to sort the array. It still uses the int type, and the other types are the same.

Same as sorting the entire array, such as

Java code

publicstaticvoidmain(String[] args) {
inta[]={2,4,1,3,7};
Arrays.sort(a);
for(inti=0;i<5;i++)//升序
System.out.println(a[i]);
}


Specify partial sorting of the array:

Java code

publicstaticvoidmain(String[] args) {
inta[]={2,4,1,3,7};
Arrays.sort(a,1,4); //输出2,1,3,4,7
for(inti=0;i<5;i++)
System.out.println(a[i]);
}


3.equals method

is used to compare whether the element values ​​​​in two arrays are equal, or to look at the int type array. Look at the Arrays source code


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)); //输出false
}


4.binarySearch method

The binarySearch method can perform binary search operations on sorted arrays. See the source code as follows


Example:

Java code

publicstaticvoidmain(String[] args) {
inta[]={2,4,1,3,7};
Arrays.sort(a);//先排序
System.out.println(Arrays.binarySearch(a, 4));//二分查找,输出3
}


5.copyof method

Copy an array. The array returned by the copyOf() method of Arrays is a new array object, so if you change the element value in the returned array, it will not affect the original array.

For example:

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();
}
}


The above is the practical implementation of Java's Arrays tool class introduced by the editor. I hope it will be helpful to everyone. If you have any If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

For more practical articles related to Java’s Arrays tool class, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn