Java Arrays类

Arrays类

介绍

用于管理或操作数组(比如排序和搜索)

常用方法

1、Arrays.toString(ints):返回数组的字符串形式

int[] ints = {1, 2, 3, 4, 5};
System.out.println(Arrays.toString(ints));

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

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

public class Array01 {
    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(ints));

        Integer[] arr = {-1, -2, 12};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        Arrays.sort(arr, new Comparator() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        System.out.println(Arrays.toString(arr));
    }
}
package com.hspedu.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, -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;
                }
            }
        }
    }
}

0482_韩顺平Java_Arrays模拟排序_哔哩哔哩_bilibili

binarySearch通过二分搜索法进行查找

要求 arrays 必须排好序,如果数组为无序的,则不能使用binarySearch方法

在使用该方法时,如果查找的元素不在数组中,则返回-(low + 1); 看下面的代码

import java.util.Arrays;

public class Array01 {
    public static void main(String[] args) {
        int[] ints = {1, 2, 3, 4, 5};

        int index = Arrays.binarySearch(ints, 122);
        // 122如果存在于数组中的话,位置应该在 元素5 的后面
        // return -(low + 1); -->   low在源码中代表元素的位置,从1开始
        // return -(5 + 1); --> -6
        System.out.println(index);
    }
}

copyOf

从arr数组中,拷贝固定长度(从头开始计数)的元素到 数组 中。新建一个数组对象

  • 如果拷贝的长度大于数组的长度,则在新数组后面添加null

  • 如果长度为负数,则抛出异常

int[] ints = {1, 2, 3, 4, 5};
int[] intsCopy = Arrays.copyOf(ints, ints.length-2);
System.out.println(Arrays.toString(intsCopy));

fill

数组填充:将数组中的每个元素都替换成一个元素

int[] ints = {1, 2, 3, 4, 5};
Arrays.fill(ints, 99);
// 输出为  [99, 99, 99, 99, 99]
System.out.println(Arrays.toString(ints));

equals

比较两个数组中的元素是否完全一致(大小和顺序均一致),如果数组元素完全一致,则返回true

int[] ints = {1, 2, 3, 4, 5};
int[] ints2 = {1, 2, 3, 5, 4};
// 输出为:  false 
System.out.println(Arrays.equals(ints, ints2));

asList

将一组值,转换为一个List集合

List asListTest = Arrays.asList("zhang", "cheng");
// 输出为   [zhang, cheng]
System.out.println(asListTest);
// asListTest的编译类型为 List
// asListTest的运行类型为: class java.util.Arrays$ArrayList
System.out.println(asListTest.getClass());

测试题

Java Arrays类_第1张图片

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

public class ArraysTest01 {
    public void sortBook(Book[] books, Comparator comparator) {
        for (int i = 0; i < books.length - 1; i++) {
            for (int j = i + 1; j < books.length; j++) {
                if (comparator.compare(books[i], books[j]) < 0) {
                    Book temp = books[i];
                    books[i] = books[j];
                    books[j] = temp;
                }
            }
        }
    }


    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦", 100.43);
        books[1] = new Book("金瓶梅", 100.45);
        books[2] = new Book("青年文摘", 5);
        books[3] = new Book("java入门到放弃", 100.49);

        ArraysTest01 arraysTest01 = new ArraysTest01();

        arraysTest01.sortBook(books, new Comparator<Book>() {
            // Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Book o1, Book o2) {
                double a = o1.getPrice();
                double b = o2.getPrice();
                System.out.println("a:" + a + " b:" + b);
                System.out.println(Math.floor(a - b));
                double v = a - b;
                if (v < 0) {
                    return 1;
                } else if (v > 0) {
                    return -1;
                } else {
                    return 0;
                }
            }
        });

        for (Book book : books) {
            System.out.println(book);
        }
    }

}

class Book {
    private String name;
    private double price;

    public Book(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Book{" + "name='" + name + '\'' + ", price=" + price + '}';
    }
}

Java Arrays类_第2张图片

你可能感兴趣的:(java,算法)