java工具类之Arrays

参考资料1 : https://www.cnblogs.com/ECJTUACM-873284962/p/7363224.html

  • java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。具有以下功能:
  • 给数组赋值:通过fill方法。
  • 对数组排序:通过sort方法,按升序。
  • 比较数组:通过equals方法比较数组中元素值是否相等。
  • 查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找法操作
package com.wpp.array;

import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;

import static org.junit.Assert.assertEquals;

/**
 * @author wpp25
 * @date 2019/10/20 10:38
 * @description:
 */
public class StudyArrays {

    /**
     * binarySerach方法
     */
    @Test
    public void binarySearch_1Test() {
        int index = Arrays.binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7}, 6);

        assertEquals(5, index);

        index = Arrays.binarySearch(new int[]{1, 2, 3, 4, 5, 6, 7}, 3, 6, 6);
        assertEquals(5, index);

        assertEquals(7, new int[]{1, 2, 3, 4, 5, 6, 7}[6]);
    }

    @Test
    public void binarySearch_1_1Test() {
        int indes = Collections.binarySearch(Arrays.asList(1, 2, 3), 2);
        assertEquals(indes, 1);

    }

    /**
     * copyOf及copyOfRange方法
     * 复制
     */
    @Test
    public void copyOfTest() {
        String[] nameArr = {"Eric", "John", "Alan", "Liz"};
        String[] copyNameArr = Arrays.copyOf(nameArr, 3);
        String[] copyNameArrRange = Arrays.copyOfRange(nameArr, 2, nameArr.length);

        Arrays.stream(copyNameArr).forEach(System.out::println);
        System.out.println("-----------");
        Arrays.stream(copyNameArrRange).forEach(System.out::println);
        System.out.println(nameArr);
        System.out.println(copyNameArr);
    }

    /**
     * sort
     */
    @Test
    public void sortTest() {
        int[] names = {4, 3, 2, 1};
        //只排序前两个
        //[John, Liz, Eric, Alan]
        Arrays.sort(names, 0, 2);
        Arrays.stream(names).forEach(System.out::println);
        //全部排序
        //[Alan, Eric, John, Liz]
        System.out.println("------------");
        Arrays.sort(names);
        Arrays.stream(names).forEach(System.out::println);

    }

    /**
     * public static  void sort(T[] a, Comparator c) {
     *         if (LegacyMergeSort.userRequested)
     *             legacyMergeSort(a, c);
     *         else
     *             TimSort.sort(a, c);
     *     }
     *     TODO
     */
    @Test
    public void sort_extend_compartorTest() {
        Date[] dataArr = {};
    }

    /**
     *Arrays的toString方法可以方便我们打印出数组内容
     */
    @Test
    public void toStringTest() {
       String[] names = {"liz","john","eric","alan"};
       Arrays.sort(names);
        System.out.println(Arrays.toString(names));// [alan, eric, john, liz]
    }

    /**
     * 如果需要打印二维数组的内容:
     * int[][] stuGrades = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } };
     * 如果直接用
     *
     * System.out.println(Arrays.toString(stuGrades));
     * 那么得到的结果类似于
     *      [[I@35ce36, [I@757aef, [I@d9f9c3]}
     * 这个时候得用 deepToString 方法才能得到正确的结果[[80, 81, 82], [84, 85, 86], [87, 88, 89]]
     */
    @Test
    public void  deepToStringTest () {
        int[][] stuGrades = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } };
        System.out.println(Arrays.deepToString(stuGrades));
    }

    @Test
    public void equalsTest() {
        String[] names1 = { "Eric", "John", "Alan", "Liz" };

        String[] names2 = { "Eric", "John", "Alan", "Liz" };

        System.out.println(Arrays.equals(names1, names2));
    }
    @Test
    public void deepEqualsTest() {
        int[][] stuGrades1 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } };

        int[][] stuGrades2 = { { 80, 81, 82 }, { 84, 85, 86 }, { 87, 88, 89 } };

        System.out.println(Arrays.deepEquals(stuGrades1, stuGrades2));
    }

    @Test
    public void  fillTest() {
        int[] array1 = new int[8];
        Arrays.fill(array1, 1);
        //[1, 1, 1, 1, 1, 1, 1, 1]
        System.out.println(Arrays.toString(array1));

        Arrays.fill(array1,3,4,9);
        System.out.println(Arrays.toString(array1));
    }

}

你可能感兴趣的:(java工具类之Arrays)