数组高级和算法

ArrayCope

public class ArrayCopeDemo {
    public static void main(String[] args) {
        int[] src = {1, 3, 5, 7, 9, 11};
        int[] dest = new int[10];
        print(dest);//[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        // 把src数组中的3,5,7,9拷贝到dest数组中去
        // arraycopy(src,1,dest,3,4); // 拷贝元素
        // 调用System类中的arraycopy方法
        System.arraycopy(src, 1, dest, 3, 4);
        print(dest); // [0, 0, 0, 3, 5, 7, 9, 0, 0, 0]
    }

    /**
     * 参数:
     *       src:源,从哪个数组中拷贝数据
     *       dest:目标,把数据拷贝到哪一个数组中
     *       srcPos:从源数组中的哪一个位置开始拷贝
     *       destPos:在目标数组中开始存放的位置
     *       length:拷贝几个元素
     */
    static void arraycopy(int[] src, int srcPos, int[] dest, int destPos, int length) {
        /**
         *      dest[destPos] = src[srcPos]; // 拷贝3
         *      srcPos++;destPos++;
         *      dest[destPos] = src[srcPos]; // 拷贝5
         *      srcPos++;destPos++;
         *      dest[destPos] = src[srcPos]; // 拷贝7
         *      srcPos++;destPos++;
         *      dest[destPos] = src[srcPos]; // 拷贝9
         *      srcPos++;destPos++;
         */
        for (int index = srcPos; index < srcPos + length; index++) {
            dest[destPos] = src[index];
            destPos++;
        }
    }

    static void print(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str = str + arr[i];
            // 不是最后一个元素
            if (i != arr.length - 1) {
                str = str + ", ";
            }
        }
        str = str + "]";
        System.out.println(str);
    }
}

ArrayInArray

// 数组中的数组
public class ArrayInArrayDemo {
    public static void main(String[] args) {
        // 元素类型[] 数组名;
        int[][] arr = new int[][]
            {
                {1, 2, 3},
                {4, 5},
                {6}
            };
        // arr2数组中有5个元素,每个元素其实是一个数组,其中有3个元素
        // int[][] arr2  = new int[5][3];
        // int[][] arr3  = new int[0][3];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.println(arr[i][j]);
            }
        }
        for (int[] xx : arr) {
            for (int x : xx) {
                System.out.println(x);
            }
        }
    }

    static void print(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str = str + arr[i];
            // 不是最后一个元素
            if (i != arr.length - 1) {
                str = str + ", ";
            }
        }
        str = str + "]";
        System.out.println(str);
    }
}

Arrays

import java.util.Arrays;

/**
 * 演示:java.util.Arrays类中的操作数组的工具方法
 */
public class ArraysDemo {
    public static void main(String[] args) {
        int[] arr = {2,9,6,7,4,1};
        // 找到java.util包下的Arrays类.
        // java.lang包下的类,不需要引入.
        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.binarySearch(arr,7));

        int[] dest = Arrays.copyOf(arr,10);
        System.out.println(Arrays.toString(dest));
    }
}

ArraySort

// 数组元素排序
public class ArraySortDemo {
    public static void main(String[] args) {
        int[] arr = {2, 9, 6, 7, 4, 1, -11, -3, 56, 34, 23, 11, 0, 11};
         print(arr);
        selectionSort(arr); // 排序
        print(arr);
    }
    // 选择排序
    static void selectionSort(int[] arr) {
        for (int count = 1; count < arr.length; count++) {
            for (int index = count; index < arr.length; index++) {
                if (arr[count - 1] > arr[index]) {
                    int temp = arr[count - 1];
                    arr[count - 1] = arr[index];
                    arr[index] = temp;
                }
            }
        }
    }

    // 冒泡排序
    static void bubbleSort(int[] arr) {
        for (int count = 1; count < arr.length; count++) {
            for (int index = 0; index < arr.length - count; index++) {
                // 前一个元素大于后一个元素
                if (arr[index] > arr[index + 1]) {
                    int temp = arr[index];
                    arr[index] = arr[index + 1];
                    arr[index + 1] = temp;
                }
            }
        }
    }

    static void print(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str = str + arr[i];
            // 不是最后一个元素
            if (i != arr.length - 1) {
                str = str + ", ";
            }
        }
        str = str + "]";
        System.out.println(str);
    }
}

ArrayUtil

/**
 * 操作数组的工具类
 * 保护了数组相关的操作方法,打印,排序,搜索,复制等等
 */
public class ArrayUtil {
    // 二分法查找
    static int binarySearch(int[] arr, int key) {
        int low = 0; // 最小索引
        int high = arr.length - 1; // 最大的索引

        while (low <= high) {
            int mid = (low + high) >>> 1; // 中间的索引
            int midValue = arr[mid]; // 中间的元素值
            if (key > midValue) {
                low = mid; // 往右移动
            } else if (key < midValue) {
                high = mid; // 往左移动
            } else {
                return mid;
            }
        }
        return -1;
    }

    // 选择排序
    static void selectionSort(int[] arr) {
        for (int count = 1; count < arr.length; count++) {
            for (int index = count; index < arr.length; index++) {
                if (arr[count - 1] > arr[index]) {
                    int temp = arr[count - 1];
                    arr[count - 1] = arr[index];
                    arr[index] = temp;
                }
            }
        }
    }
// 冒泡排序 static void bubbleSort(int[] arr) { for (int count = 1; count < arr.length; count++) { for (int index = 0; index < arr.length - count; index++) { // 前一个元素大于后一个元素 if (arr[index] > arr[index + 1]) { int temp = arr[index]; arr[index] = arr[index + 1]; arr[index + 1] = temp; } } } } static void print(int[] arr) { String str = "["; for (int i = 0; i < arr.length; i++) { str = str + arr[i]; // 不是最后一个元素 if (i != arr.length - 1) { str = str + ", "; } } str = str + "]"; System.out.println(str); } }

 BinarySeach

public class BinarySeachDemo {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        // 从arr数组中查找出8的位置.
        int index = binarySearch(arr, 5);
        System.out.println(index);
    }
    /**
     *      参数:
     *           arr:表示从哪一个数组中做查找
     *           key:表示查询哪一个元素
     *      返回:
     *           返回该元素出现的索引位置.
     *           若不存在,则返回-1.
     */
    static int binarySearch(int[] arr, int key) {
        int low = 0; // 最小索引
        int high = arr.length - 1; // 最大的索引
        while (low <= high) {
            int mid = (low + high) >>> 1; // 中间的索引
            int midValue = arr[mid]; // 中间的元素值
            if (key > midValue) {
                low = mid; // 往右移动
            } else if (key < midValue) {
                high = mid; // 往左移动
            } else {
                return mid;
            }
        }
        return -1;
    }
}

 ForEach

public class ForEachDemo {
    public static void main(String[] args) {
        String[] arr = {"A", "B", "C", "D"};
        // 取出数组中的每一个元素
        // 传统的方式:使用循环
        for (int index = 0; index < arr.length; index++) {
            String ele = arr[index];
            System.out.println(ele);
        }
        System.out.println("-------------------------");
        // 增强for循环
        for (String ele : arr) {
            System.out.println(ele); // 元素
        }
        int[] arr2 = {1, 3, 5, 7, 9};
        for (int x : arr2) {
            x = 1111;
            System.out.println(x);
        }
        int num = 1_2_3_4;
    }
}

 Param

// 方法参数的值传递机制之基本数据类型
public class ParamDemo1 {
    public static void main(String[] args) {
        // 案例:定义一个方法change,改变传入的参数的值。
        int x = 16;
        System.out.println("x=" + x); // 16
        change(x); // 改变
        System.out.println("x=" + x); // 16
    }

    // 改变传入的参数的值
    static void change(int x) {
        System.out.println("x1=" + x);
        x = 99;
        System.out.println("x2=" + x);
    }
}
// 方法参数传递机制之引用类型
public class ParamDemo2 {
    public static void main(String[] args) {
        /**
         * 案例:定义一个方法swap,用于交换数组中第一个和最后一个元素的值。
         *          交换之前:[17,98]
         *          交换之后:[98,17]
         */
        int[] arr = {17, 98};
        print(arr); // 交换之前:[17,98]
        // 交换
        swap(arr, 0, arr.length - 1);
        print(arr); // 交换之后:[98,17]
    }

    static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

    // 打印数组
    static void print(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length; i++) {
            str = str + arr[i];
            // 不是最后一个元素
            if (i != arr.length - 1) {
                str = str + ", ";
            }
        }
        str = str + "]";
        System.out.println(str);
    }
}

VarArgs

// 方法的可变参数
public class VarArgsDemo {
    public static void main(String[] args) {
        // muns数组中,存储的是商品的单价
        double[] prices = {10, 30, 50, 70};
        double total = getTotalPrice(0.8, 10, 30, 50, 70);
        System.out.println("商品总价=" + total); // 商品价格=128.0

        getSum(1, 2); // 2
        getSum(1, 2, 3); // 3
    }

    static int getSum(int... nums) {
        System.out.println(nums.length);
        return 0;
    }

    static double getTotalPrice(double cutoff, double... nums) {
        double total = 0.0;
        for (double price : nums) {
            total += price; // total= total+price;
        }
        return total * cutoff;
    }

    // 定义一个方法,求出数组中的所有单价的总和.
    static double getTotalPrice(double[] nums) {
        double total = 0.0;
        for (double price : nums) {
            total += price; // total= total+price;
        }
        return total;
    }
}
原文地址:https://www.cnblogs.com/zengqinghong/p/11827864.html