package com.lovecoding.day16.api03;

import org.junit.Test;

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

public class Test01Arr {

    //数组扩容
    @Test
    public void test11(){
        int[] arr1 = new int[10];
        arr1 = Arrays.copyOf(arr1, (int) (arr1.length * 1.5));
        System.out.println(arr1.length);
    }

    @Test
    public void test12(){
        int[] arr1 = new int[10];
        //扩容1.5倍
        // 0000 1010  /2  5
        // 10 +_10 / 2
        int i = arr1.length + (arr1.length >> 1); // >> 右移 和 + 优先级相同
        //int i1 = arr1.length + arr1.length / 2 ;

        arr1 = Arrays.copyOf(arr1,i );
        System.out.println(arr1.length);
    }

    @Test
    public void test13(){
        int[] arr1 = new int[10];
        //扩容2倍
        int i =  (arr1.length << 1); // >> 右移 和 + 优先级相同
        arr1 = Arrays.copyOf(arr1,i );
        System.out.println(arr1.length);
    }

    //System.arraycopy(original, 0, copy, 0,
    //                         Math.min(original.length, newLength));

    /*
        static void arraycopy​(Object src, int srcPos, Object dest, int destPos, int length)
        将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。
        参数
        src - 源数组。
        srcPos - 源数组中的起始位置。
        dest - 目标数组。
        destPos - 目标数据中的起始位置。
        length - 要复制的数组元素的数量。
     */
    // 1. 数组的移动  1,2,3,4,5    1,1,2,3,4   1,2,2,3,4
    // 2. 数组的移动  1,2,3,4,5    2,3,4,5,5   1,2,3,4,4
    @Test
    public void test10(){
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = new int[5];

        //1,2,3,4,5  0,1,2,3,4
        //System.arraycopy(arr1,0,arr2,1,4);

        //1,2,3,4,5  1,0,2,3,4
        //System.arraycopy(arr1,1,arr2,2,3);
        //System.arraycopy(arr1,0,arr2,0,1);

        //1,2,3,4,5  2,3,4,5,0
        //System.arraycopy(arr1,1,arr2,0,4);

        //1,2,3,4,5  1,2,3,4,4
        System.arraycopy(arr1,0,arr2,0,4);
        System.arraycopy(arr1,3,arr2,4,1);
        System.out.println(Arrays.toString(arr2));

    }



    @Test
    public void test09(){
        int[] arr1 = {1,2,3,4,5};
        //删除数组[0]位置的元素 1,2,3,4,5 都向前移动  【2,3,4,5】
        System.arraycopy(arr1,1,arr1,0,4);

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

        String[] strings = {"hello","java","world","lovecoding","xerga"};
        int total = strings.length;

        //删除[0]位置元素
//        int index = 0;
//        System.arraycopy(strings, index+1,strings, index,total-index-1);
//        strings[--total] = null;
//        System.out.println(Arrays.toString(strings));

        //删除[2]位置元素
        int index = 2;
        System.arraycopy(strings, index+1,strings, index, total-index-1);;
        strings[--total] = null;
        System.out.println(Arrays.toString(strings));

    }
    //数组[index]位置插入新元素 整体向后移动
    @Test
    public void test08(){

        String[] strings = {"hello","java","world",null,null};
        int total = 3;
        //在[0]位置插入"haha"
        int index = 0;
        System.arraycopy(strings, index,strings, index+1,total-index);
        total++;
        strings[index] = "haha";
        System.out.println(Arrays.toString(strings));

    }
    @Test
    public void test07(){
        int[] arr1 = {1,2,3,4,5};

        //修改数组为 0 0 1 2 3
        System.arraycopy(arr1,0,arr1,2,3);
        arr1[0] = 0;
        arr1[1] = 0;

        System.out.println(Arrays.toString(arr1));
    }

    @Test
    public void test06(){
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = new int[10];

        System.arraycopy(arr1,0,arr2,2,arr1.length);

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

    }


    //* 数组元素的二分查找
    //  * static int binarySearch(int[] a, int key)
    //  * static int binarySearch(Object[] a, Object key) ：要求数组有序，在数组中查找key是否存在，如果存在返回第一次找到的下标，不存在返回负数
    @Test
    public void test02(){
        int[] arr = {1,3,5,2,11,9,1,12,32,32,31,23,12,4,5,4,5,436,45,7,65,7,6,8,76,85,45,7,56};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        int i = Arrays.binarySearch(arr, 23);
        System.out.println(i);

        System.out.println(arr[i]);

        //了解
        Student[] arr2 = {new Student(1),new Student(2),new Student(3)};
        int i1 = Arrays.binarySearch(arr2, new Student(2));
        System.out.println(i1);


    }

    //* 数组的复制
    //  * static int[] copyOf(int[] original, int newLength)  ：根据original原数组复制一个长度为newLength的新数组，并返回新数组
    //  * static <T> T[] copyOf(T[] original,int newLength)：根据original原数组复制一个长度为newLength的新数组，并返回新数组
    //  * static int[] copyOfRange(int[] original, int from, int to) ：复制original原数组的[from,to)构成新数组，并返回新数组
    //  * static <T> T[] copyOfRange(T[] original,int from,int to)：复制original原数组的[from,to)构成新数组，并返回新数组
    @Test
    public void test03(){
        int[] arr = {1,3,5,2,11,9,1,12,32,32,31,23};

        int[] ints = Arrays.copyOf(arr, 100);
        System.out.println(Arrays.toString(ints));

        int[] ints1 = Arrays.copyOfRange(arr, 0, 3);
        System.out.println(Arrays.toString(ints1));


    }

    //* 比较两耳数组是否相等
    //  * static boolean equals(int[] a, int[] a2) ：比较两个数组的长度、元素是否完全相同
    //  * static boolean equals(Object[] a,Object[] a2)：比较两个数组的长度、元素是否完全相同
    @Test
    public void test04(){
        int[] arr1 = {1,3,5,2,1};
        int[] arr2 = {1,3,5,2,1};

        System.out.println(Arrays.equals(arr1, arr2));
    }

    //* 填充数组
    //  * static void fill(int[] a, int val) ：用val值填充整个a数组
    //  * static void fill(Object[] a,Object val)：用val对象填充整个a数组
    //  * static void fill(int[] a, int fromIndex, int toIndex, int val)：将a数组[fromIndex,toIndex)部分填充为val值
    //  * static void fill(Object[] a, int fromIndex, int toIndex, Object val) ：将a数组[fromIndex,toIndex)部分填充为val对象

    @Test
    public void test05(){
        int[] arr = new int[10];

//        Arrays.fill(arr,8);
//        System.out.println(Arrays.toString(arr));

        Arrays.fill(arr,2,4,11);
        System.out.println(Arrays.toString(arr));

    }


    @Test
    public void test01(){
        //数组排序
        //* static void sort(int[] a) ：将a数组按照从小到大进行排序
        //* static void sort(int[] a, int fromIndex, int toIndex) ：将a数组的[fromIndex, toIndex)部分按照升序排列
        //* static void sort(Object[] a) ：根据元素的自然顺序对指定对象数组按升序进行排序。
        //* static <T> void sort(T[] a, Comparator<? super T> c) ：根据指定比较器产生的顺序对指定对象数组进行排序。

        int[] arr = {1,3,5,2,11,9};
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));

        //static void sort(Object[] a)
        Student[] arr1 = {new Student(11),new Student(12),new Student(10)};
        Arrays.sort(arr1);
        System.out.println(Arrays.toString(arr1));

        System.out.println("--------------");
        //static <T> void sort(T[] a, Comparator<? super T> c)
        Person[] arr2 = {new Person(10),new Person(9),new Person(12)};

        Arrays.sort(arr2, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.age.compareTo(o2.age);
            }
        });
        System.out.println(Arrays.toString(arr2));

    }
}

class Student implements Comparable{
    public Student(Integer age) {
        this.age = age;
    }

    Integer age;
    @Override
    public int compareTo(Object o) {
        return this.age.compareTo(((Student) o).age);
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }
}

class Person {

    Integer age;

    public Person(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                '}';
    }
}