Arrays常用方法
toString
返回数组的字符串形式
Integer[] integers = {1, 20, 90};
// 遍历数组
for(int i = 0; i < integers.length; i++) {
System.out.println(integers[i]);
}
// 直接使用Arrays.toString方法,显示数组
System.out.println(Arrays.toString(integers));
sort
排序(自然排序和定制排序——后面详见)
int[] a1 = {2,3,1,0,34,-1};
Arrays.sort(a1);
for (Object o :a1) {
System.out.println(o);
}
binarySearch
通过二分搜索法进行查找,要求必须排好序
int[] a1 = {2,3,1,0,34,-1};
Arrays.sort(a1);
for (Object o :a1) {
System.out.println(o);
}
System.out.println(Arrays.toString(a1));
System.out.println("=========");
System.out.println(Arrays.binarySearch(a1,2));
//如果数组中不存在该元素,就返回 return -(low + 1); // key not found.
copyOf
数组元素的复制
int[] arr = {1,2,3,1,3};
//1. 从 arr 数组中,拷贝 arr.length个元素到 newArr数组中
//2. 如果拷贝的长度 > arr.length 就在新数组的后面 增加 null
//3. 如果拷贝长度 < 0 就抛出异常NegativeArraySizeException
//4. 该方法的底层使用的是 System.arraycopy()
Integer[] newArr = Arrays.copyOf(arr, arr.length);
System.out.println("==拷贝执行完毕后==");
System.out.println(Arrays.toString(newArr));
fill
数组元素的填充
Integer[] num = new Integer[]{9,3,2};
//1. 使用 99 去填充 num数组,可以理解成是替换原理的元素
Arrays.fill(num, 99);
System.out.println("==num数组填充后==");
System.out.println(Arrays.toString(num));
asList
将一组值,转换成list
//1. asList方法,会将 (2,3,4,5,6,1)数据转成一个List集合
//2. 返回的 asList 编译类型 List(接口)
//3. asList 运行类型 java.util.Arrays#ArrayList, 是Arrays类的
// 静态内部类 private static class ArrayList<E> extends AbstractList<E>
// implements RandomAccess, java.io.Serializable
List asList = Arrays.asList(2,3,4,5,6,1);
System.out.println("asList=" + asList);
System.out.println("asList的运行类型" + asList.getClass());
sort排序源码分析
Integer arr[] = {1, -1, 7, 0, 89};
//进行排序
//1. 可以直接使用冒泡排序 , 也可以直接使用Arrays提供的sort方法排序
//2. 因为数组是引用类型,所以通过sort排序后,会直接影响到 实参 arr
//3. sort重载的,也可以通过传入一个接口 Comparator 实现定制排序
//4. 调用 定制排序 时,传入两个参数 (1) 排序的数组 arr
// (2) 实现了Comparator接口的匿名内部类 , 要求实现 compare方法
//5. 先演示效果,再解释
//6. 这里体现了接口编程的方式 , 看看源码,就明白
// 源码分析
//(1) Arrays.sort(arr, new Comparator()
//(2) 最终到 TimSort类的 private static <T> void binarySort(T[] a, int lo, int hi, //int start,
Comparator<? super T> c)()
//(3) 执行到 binarySort方法的代码, 会根据动态绑定机制 c.compare()执行我们传入的
// 匿名内部类的 compare ()
while (left < right) {
int mid = (left + right) >>> 1;
if (c.compare(pivot, a[mid]) < 0)
right = mid;
else
left = mid + 1;
}
(4) new Comparator() {
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i2 - i1;//可以根据要求进行从大到小或从小到大排序
}
}
(5) public int compare(Object o1, Object o2) 返回的值>0 还是 <0
/** 会影响整个排序结果, 这就充分体现了 接口编程+动态绑定+匿名内部类的综合使用
将来的底层框架和源码的使用方式,会非常常见*/
Arrays.sort(arr); // 默认排序方法
定制排序
这里运用到了匿名内部类,把匿名内部类当做一个对象更容易理解下面代码
这里代码是现进入sort方法中,在sort方法中含有Comparator方法,根据动态绑定机制,代码会进行匿名内部类中compare方法,return可以进行从大到小或从小到大排序
示例1:
Integer arr[] = {1, -1, 7, 0, 89};
//定制排序
Arrays.sort(arr, new Comparator() {//这里运用了匿名内部类
@Override
public int compare(Object o1, Object o2) {
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return i2 - i1;
}
});
System.out.println("===排序后===");
System.out.println(Arrays.toString(arr));
示例2:
Integer[] arr={5,3,6,1,4};
//使用的排序算法是二叉树排序
// Arrays.sort(arr);//默认排序,升序排序
//自定义排序,默认是升序排序
//根据匿名内部类返回值的正负来确定降序还是升序排序
Arrays.sort(arr, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// return o2-o1;//降叙排序
return o1-o2;//升序排序
}
});
System.out.println(Arrays.toString(arr));
下面代码是sort中compare方法
// 匿名内部类的 compare ()
while (left < right) {
int mid = (left + right) >>> 1;
if (c.compare(pivot, a[mid]) < 0)
right = mid;
else
left = mid + 1;
定制排序练习演示
结合冒泡排序进行定制排序
public class ArraysSortCustom {
public static void main(String[] args) {
int[] arr = {1, -1, 8, 0, 20};
//bubble01(arr);
bubble02(arr, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
int i1 = (Integer) o1;
int i2 = (Integer) o2;
return i2 - i1;// return i2 - i1;
}
});
System.out.println("==定制排序后的情况==");
System.out.println(Arrays.toString(arr));
}
//传统方法
//使用冒泡完成排序
// public static void bubble01(int[] arr) {
// int temp = 0;
// for (int i = 0; i < arr.length - 1; i++) {
// for (int j = 0; j < arr.length - 1 - i; j++) {
// //从小到大
// if (arr[j] > arr[j + 1]) {
// temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
// }
// }
// }
// }
//结合冒泡 + 定制
public static void bubble02(int[] arr, Comparator c) {
int temp = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
//数组排序由 c.compare(arr[j], arr[j + 1])返回的值决定
if (c.compare(arr[j], arr[j + 1]) > 0) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
排序实战演示
**案例:**自定义Book类,里面包含name和price,按price排序(从大到小。要求使用两种方式排序,对对象的某个属性排序,有一个Book[] books = 5本书对象。使用前面学习过的传递实现Comparator接口匿名内部类,也称为定制排序。
**要求:**按照price(1)从大到小 (2)从小到大 (3)按照书名长度从大到小
public class ArrayExercise {
public static void main(String[] args) {
Book[] books=new Book[4];
books[0] = new Book("红楼梦", 100);
books[1] = new Book("金瓶梅", 90);
books[2] = new Book("青年文摘", 5);
books[3] = new Book("java从入门到放弃", 300);
// //按照price的升序排序
// Arrays.sort(books, new Comparator<Book>() {
// //对book数组进行排序,以此 o1和o2 就是Book对象
// @Override
// public int compare(Book o1, Book o2) {
// //判断大小
// double priceVla = o1.getPrice() - o2.getPrice();
// //排序的方式按照price的降序或升序
// if (priceVla>0){
// return 1;
// }else if (priceVla<0){
// return -1;
// }else{
// return 0;
// }
// }
// });
// //按照price的降序排序
// Arrays.sort(books, new Comparator<Book>() {
// //对book数组进行排序,以此 o1和o2 就是Book对象
// @Override
// public int compare(Book o1, Book o2) {
// //判断大小
// double priceVla = o1.getPrice() - o2.getPrice();
// //排序的方式按照price的降序或升序
// if (priceVla>0){
// return -1;
// }else if (priceVla<0){
// return 1;
// }else{
// return 0;
// }
// }
// });
//按照书名的长度降序排序
Arrays.sort(books, new Comparator<Book>() {
//对book数组进行排序,以此 o1和o2 就是Book对象
@Override
public int compare(Book o1, Book o2) {
return o2.getName().length()-o1.getName().length();
}
});
System.out.println(Arrays.toString(books));
}
}
//书类
class Book{
private String name;
private double price;
public Book(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}