Arrays常用类

140 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

Arrays常用类

  • Arrays里面包含了一系列静态方法,用于管理或操作数组(比如排序和搜索)。
  1. toString返回数组的字符串形式

    Arrays.toString(arr)

  2. sort排序(自然排序和定制排序)

    Integer arr[] = {1,-1, 7, 0, 89};

  3. binarySearch通过二分搜索法进行查找,要求必须排好序

    int index = Arrays.binarySearch(arr 3);

  4. copyOf数组元素的复制

Integer[] newArr = Arrays.copyOf(arr, arr.length);

  1. fill数组元素的填充

Integer[] num = new Integer[]{9,3,2}; Arrays.fill(num, 99);

  1. equals比较两个数组元素内容是否完全一致

boolean equals = Arrays.equals(arr, arr2);

  1. asList将一组值,转换成list

List asList = Arrays.asList(2,3,4,5,6,1); System.out.println(" asList=" + asList);

方法演示

package www.xz.arrays_;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 许正
 * @version 1.0
 */
public class ArraysMethod01 {
    public static void main(String[] args) {

        Integer[] integers = {1, -1, 7, 0, 89};
        for (int i = 0; i < integers.length; i++) {
            System.out.println(integers[i]);
        }

        System.out.println(Arrays.toString(integers));


        //进行排序
        //1.可以直接使用冒泡排序,也可以直接使用Arrays提供的sort方法排序
        //2.因为数组是引用类型,所以通过sort排序后,会直接影响到实参arr
        //3.sort重载的,也可以通过传入一个接口 Comparator 实现定制排序
        //4.调用定制排序时,传入两个参数 (1)排序的数组integers
        //                          (2)实现了Comparator接口的匿名内部类,要求实现 compare 方法
//        Arrays.sort(integers);//默认排序方法

        //定制排序
        Arrays.sort(integers, 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(integers));//
    }
}
package www.xz.arrays_;

import java.util.Arrays;
import java.util.List;

/**
 * @author 许正
 * @version 1.0
 */
public class ArraysMethod02 {
    public static void main(String[] args) {
        Integer arr[] = {1, 2, 3, 8, 64, 253, 589};
        // binarySearch 通过二分 搜索法进行查找,要求必须排好
        //1. 使用binarySearch 二叉查找
        //2.要求该数组是有序的。如果该数组是无序的,不能使用binarySearch
        //3.如果数组中不存在该元素,就返回 return - (low + 1); // key not found.
        //                            即应该所在的位置+1的负数
        int index = Arrays.binarySearch(arr, 254);
        System.out.println(index);

        //copyOf数组元素的复制
        //1.从arr数组中,拷贝arr.length个元素到newArr数组中
        //2.如果拷贝的长度> arr.Length 就在新数组的后面增加null
        //3. 如果拷贝长度< 0就抛出异常NegativeArraySizeException
        //4.该方法的底层使用的是System.arraycopy()
        Integer[] newArr = Arrays.copyOf(arr, arr.length + 1);
        System.out.println("===拷贝执行完毕===");
        System.out.println(Arrays.toString(newArr));


        //fi11数组元素的填充
        Integer[] num = new Integer[]{9, 3, 2};
        //1.使用99去填充num数组,可以理解成是替换原来的元素
        Arrays.fill(num, 99);
        System.out.println("==num数组填充后==");
        System.out.println(Arrays.toString(num));


        //equals 比较两个数组元素内容是否完全一致
        Integer[] arr2 = {1, 2, 3, 8, 64, 253, 589};
        //1. 如果arr和arr2数组的元素样,则方法true;
        //2. 如果不是完全一样,就返回false
        boolean equals = Arrays.equals(arr, arr2);
        System.out.println("equals=" + equals);


        //asList将一组值, 转换成List
        //1. asList方法, 会将(2,3,4,5,6,1)数据转成一个List集合
        //2.返回的 asList 编译类型List(接口)
        //3. asList 运行类型  class java.util.Arrays$ArrayList
        List asList = Arrays.asList(2, 3, 4, 5, 6, 1);
        System.out.println("asList=" + asList);
        System.out.println("asList的运行类型" + asList.getClass());
    }
}

自定义排序

package www.xz.arrays_;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author 许正
 * @version 1.0
 */
public class ArraysSortCustom {
    public static void main(String[] args) {

        int arr[] = {1, 20, 50, -5, 0, 23};
//        bubble01(arr);


        bubble02(arr, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Integer i1 = (Integer) o1;
                Integer i2 = (Integer) o2;
                return i1 - i2;
            }
        });

        System.out.println(Arrays.toString(arr));

    }

    //使用冒泡完成排序
    public static void bubble01(int arr[]) {
        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]) {
                    int temp = 0;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    //结合冒泡 + 定制
    public static void bubble02(int[] arr, Comparator c) {
        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]) > 0 返回的值来决定
                if (c.compare(arr[j], arr[j + 1]) > 0) {
                    int temp = 0;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
}

练习

package www.xz.arrays_;

import java.util.Arrays;
import java.util.Comparator;
import java.util.SplittableRandom;

/**
 * @author 许正
 * @version 1.0
 */
public class ArraysExercise {
    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);

        //(1)按照价格从大到小排序
//        Arrays.sort(books, new Comparator() {
//            @Override
//            public int compare(Object o1, Object o2) {
//                Book b1 = (Book) o1;
//                Book b2 = (Book) o2;
//                double priceVal = b1.getPrice() - b2.getPrice();
//                if (priceVal > 0) {
//                    return -1;
//                } else if (priceVal < 0) {
//                    return 1;
//                } else {
//                    return 0;
//                }
//            }
//        });
//        System.out.println(Arrays.toString(books));


        //(2)按照价格从小到大排序
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Book b1 = (Book) o1;
                Book b2 = (Book) o2;
                double priceVal = b1.getPrice() - b2.getPrice();
                if (priceVal > 0) {
                    return 1;
                } else if (priceVal < 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });
        System.out.println(Arrays.toString(books));


        //(3)按照书名长度从小到大排序
        Arrays.sort(books, new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                Book book1 = o1;
                Book book2 = o2;
                return book1.getName().length() - book2.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 +
                '}';
    }
}