Arrays类中的方法多为static进行修饰,所以不用new对象。下面是一些常用方法:
fill给数组赋值
sort给数组排序(正序)
equals比较数组
binarySearch查找数组元素
......
package com.xzc.arrays;
import java.util.Arrays;//函数多为static开头 可以直接调用
public class Array {
public static void main(String[] args){
int[] array = {1, 2, 3, 999, 46, 789};
//打印数组元素
System.out.println(Arrays.toString(array));//[1, 2, 3, 999, 46, 789]
//给数组排序
Arrays.sort(array);
System.out.println(Arrays.toString(array));//[1, 2, 3, 46, 789, 999]
//给数组赋值
Arrays.fill(array, 3);
System.out.println(Arrays.toString(array));//[3, 3, 3, 3, 3, 3]
Arrays.fill(array, 2, 5, 1);
System.out.println(Arrays.toString(array));//[3, 3, 1, 1, 1, 3]
//比较数组
int[] b = {3, 3, 1, 1, 1, 3};
int[] c = {1, 3, 1, 1, 1, 3};
boolean bool1 = Arrays.equals(array, b);
boolean bool2 = Arrays.equals(array, c);
System.out.println(bool1);//true
System.out.println(bool2);//false
//查找数组(先排序后使用binarySearch)
int[] s = {1, 2, 5, 7, 9};
int seek = Arrays.binarySearch(s, 5);
System.out.println(seek);//2
}
}