Arrays类概述
常用API
返回数组内容的函数
Arrays.toString(数组名)
String[] a={"akdsjka","dkjajdk","dakjjkd"};
String array= Arrays.toString(a);
System.out.println(array);
排序(默认升序)
Arrays.sort(数组名)
public static void main(String[] args) {
String[] a={"akdsjka","dkjajdk","dakjjkd"};
Arrays.sort(a);
String array=Arrays.toString(a);
System.out.println(array);
}
二分搜索(二分法只对有序序列有效)
Arrays.binarySearch(数组名,要搜索的值)
返回的值是存在元素的下标,-(存在元素应该插入位置索引+1)
public static void main(String[] args) {
String[] a={"akdsjka","dkjajdk","dakjjkd"};
Arrays.sort(a);
int nun=Arrays.binarySearch(a,"akdsjka");
String array=Arrays.toString(a);
System.out.println(array);
System.out.println(nun);
}
自定义排序规则(只支持引用类型)
自定义比较器对象
public static void main(String[] args) {
Integer[] a1={1,2,3,4};
Arrays.sort(a1, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
String n=Arrays.toString(a1);
System.out.println(n);
}