Java數組和數據工具類Arrays的使用

package com.example.demo;

import org.junit.Test;

import java.util.Arrays;

/**
 * @Description 數組工具類Arrays的使用
 * @Auther gf.x
 * @Date 2020/5/16 18:58
 */
public class TestArrays {

    //1. System.arraycopy()數組複製方法
    @Test
    public void test() {
        int[] a1 = {1, 2, 3, 4, 5, 6};
        int[] a2 = {11, 12, 13, 14, 15, 16};

        System.arraycopy(a1, 2, a2, 3, 2);
        System.out.println(Arrays.toString(a2)); //[11, 12, 13, 3, 4, 16]
    }

    //2. 二分法查找Arrays.binarySearch()方法
    @Test
    public void test1() {
        int[] a = {1, 2, 323, 23, 543, 12, 59};
        Arrays.sort(a); //使用二分法查找,必須先對數組進行排序
        System.out.println(Arrays.toString(a)); //[1, 2, 12, 23, 59, 323, 543]

        //查找某個元素是否在指定數組中,存在返回它的索引,否則返回負數
        System.out.println(Arrays.binarySearch(a, 12)); //2
        System.out.println(Arrays.binarySearch(a, 1112)); //-8
    }

    //3. 數組填充Arrays.fill(arrayname,value),
	//Arrays.fill(arrayname ,starting index ,ending index ,value)
    @Test
    public void test2() {
        int[] a = new int[6];

        Arrays.fill(a, 100);
        System.out.println(Arrays.toString(a)); //[100, 100, 100, 100, 100, 100]

        Arrays.fill(a, 3, 6, 50);
        System.out.println(Arrays.toString(a)); //[100, 100, 100, 50, 50, 50]
    }

    //4. 冒泡排序
    @Test
    public void test3() {
        int[] a2 = {1, 2, 323, 23, 543, 12, 59};
        bubbleSort(a2);

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

    //冒泡排序算法實現
    private void bubbleSort(int[] a2) {
        int temp;
        /**
         * 外層遍歷第一遍先找出最大的,然後遍歷第二遍找出第二大的,依次...最後找出最小的
         *
         * 外層負責遍歷取出數組中的每個元素
         */
        for (int i = 0; i < a2.length; i++) {
            /**
             * 數組中遍歷第一遍第一個元素和第二個元素比較,大的後移;
             * 然後遍歷第二遍第二個元素再和第三個元素比較,大的再後移...依次類推
             *
             * 外層遍歷第一遍內層遍歷 a2.length - 1 遍找出最大的元素
             * 內層負責元素(遍歷)比較排序
             */
            for (int j = 0; j < a2.length - 1 - i; j++) {
                if (a2[j] > a2[j + 1]) {
                    temp = a2[j];
                    a2[j] = a2[j + 1];
                    a2[j + 1] = temp;
                }
            }
        }
    }

    //5. 二分法查找算法(自己實現)
    @Test
    public void test6() {
        int[] a = {1, 2, 323, 23, 543, 12, 59};
        Arrays.sort(a); //使用二分法查找,必須先對數組進行排序
        System.out.println(a);

        int result = dichotomySearch(a, 0, a.length, 23);
        System.out.println(result);
    }

    //二分法查找算法實現
    private int dichotomySearch(int[] a, int fromIndex, int toIndex,
                                int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }


    ////////////////////////////// 其他方法 ///////////////////////////////////

    //6. 判斷兩個數組內容是否相等 (Arrays工具類提供了這個方法)
    @Test
    public void test5() {
        int[] a1 = {1, 2, 3, 4, 5, 6};
        int[] a2 = {11, 12, 13, 14, 15, 16};
        int[] a3 = {11, 12, 13, 14, 15, 16};

        System.out.println(arrayEquals(a1, a2));
        System.out.println(arrayEquals(a2, a3));
    }

    //判斷兩個數組內容是否相等的實現 (類似於字符串equals方法)
    private boolean arrayEquals(int[] a, int[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

        int length = a.length;
        if (a2.length != length)
            return false;

        for (int i=0; i<length; i++)
            if (a[i] != a2[i])
                return false;

        return true;
    }

    //7.  >> << 位運算
    @Test
    public void test4() {
        int r = 5 >> 1;
        int r1 = -5 >> 1;
        System.out.println(r); //2
        System.out.println(r1); //-3
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章