java - Arrays 工具类

常用方法

  • binarySearch(byte[] a, byte key) 使用二分搜索法来搜索指定的 byte 型数组,以获得指定的值。
  • binarySearch(byte[] a, int fromIndex, int toIndex, byte key) 使用二分搜索法来搜索指定的 byte 型数组的范围,以获得指定的值。
  • copyOf(byte[] original, int newLength) 复制指定的数组,截取或用 0 填充(如有必要),以使副本具有指定的长度。
  • copyOfRange(boolean[] original, int from, int to) 将指定数组的指定范围复制到一个新数组。
  • equals(byte[] a, byte[] a2) 如果两个指定的 byte 型数组彼此相等,则返回 true
  • fill(byte[] a, byte val) 将指定的 byte 值分配给指定 byte 节型数组的每个元素。
  • sort(byte[] a) 对指定的 byte 型数组按数字升序进行排序。
  • sort(byte[] a, int fromIndex, int toIndex) 对指定 byte 型数组的指定范围按数字升序进行排序。
  • toString(byte[] a) 返回指定数组内容的字符串表示形式。

在使用Arrays工具类的二分法查找时,由于二分法的原理(先将数组一分为二,再与其中间元素进行对比,按对比结果判断要查找的元素在哪一半,直到找到该元素,返回下标),所以必须先将该数组进行排序,否则所找到的结果并无意义。

public int myBinarySearch(int[] arr, int obj) {
    //查找长度
    int len = arr.length;
    //要进行比较的元素
    int index = (len-1)/2;
    //数组边界(元素所在位置为arr[bound, arr.length - 1])
    int bound = 0;                
    
    while (true) {
        if (arr[index]<obj) {
            //例如查找的元素为5, 而arr[index] = 3,arr[index + 1] = 6,说明5并不存在数组中
            if(arr[index+1]>obj) {
                return -1;
            }
            bound = index;
            index = len - index;
            index /= 2;
            index = bound + index;
        } else if (arr[index]>obj) {
            //与arr[index]<obj情况相同
            if(arr[index-1]<obj) {
                return -1;
            }
            index /= 2;                
        } else {
            //判断元素是否重复,重复则找到第一个出现的元素
            while (arr[index-1]==obj) {
                index--;
            }
            return index;
        }
        //判断查找范围是否越界
        if (index<0 || index>arr.length-1) {
            return -1;
        }
        //判断元素首部和尾部是否为该元素,是则返回index
        if (index==0 || index==arr.length-1) {
            if(arr[index]==obj) {
                return index;
            } else {
                return -1;
            }
        }
    }                
}

 

import java.util.Arrays;
import java.util.List;

/**
 * fileName: ArrayTester
 * description: 使用Arrays工具类
 *
 * @author lihaogn-main
 * @version 1.0
 * @date 2019/9/10 20:14
 */
public class ArrayTester {

    public static void main(String[] args) {

        // 1 生成list
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
        List<String> list1 = Arrays.asList("zhangsan", "lisi", "wangwu");
        System.out.println(list); // [1, 2, 3, 4, 5, 6]
        System.out.println(list1); // [zhangsan, lisi, wangwu]

        // 2 二分搜索,返回下标,没有找到返回-1
        int[] a = {1, 2, 3, 4, 5, 6};
        int aa = Arrays.binarySearch(a, 2);
        int aaa = Arrays.binarySearch(a, 0);
        System.out.println(aa); // 1
        System.out.println(aaa); // -1

        // 3 拷贝数组
        int[] b = Arrays.copyOf(a, a.length);
        System.out.println(a + ": " + Arrays.toString(a)); 
        // [I@1540e19d: [1, 2, 3, 4, 5, 6]
        System.out.println(b + ": " + Arrays.toString(b)); 
        // [I@677327b6: [1, 2, 3, 4, 5, 6]

        // 拷贝下标[2,a.length)之间的数
        int[] c = Arrays.copyOfRange(a, 2, a.length);
        System.out.println(Arrays.toString(c)); // [3, 4, 5, 6]

        // 4 比较两个数组内容是否相同
        System.out.println(Arrays.equals(a, b)); // true

        // 5 填充数组
        int[] d = new int[5];
        Arrays.fill(d, 5);
        System.out.println(Arrays.toString(d)); // [5, 5, 5, 5, 5]

        // 填充数组e下标[1,3)的值
        int[] e = new int[5];
        Arrays.fill(e, 1, 3, -1);
        System.out.println(Arrays.toString(e)); // [0, -1, -1, 0, 0]

        /*
         * Cumulates, in parallel, each element of the given array in place, 
         * using the supplied function.
         * 
         * 6 使用提供的函数并行地累积给定数组中的每个元素
         */
        Arrays.parallelPrefix(e, (x, y) -> x + y);
        System.out.println(Arrays.toString(e)); // [0, -1, -2, -2, -2]
        Arrays.parallelPrefix(e, (x, y) -> x * 2 + y);
        System.out.println(Arrays.toString(e)); // [0, -1, -4, -10, -22]

        /*
         * Sorts the specified array into ascending numerical order.
         * 
         * 7 将指定数组按升序排列,并行
         * */
        Arrays.parallelSort(e);
        System.out.println(Arrays.toString(e)); // [-22, -10, -4, -1, 0]

        int[] f = {3, 67, 1, 34, 76, 2, 13};
        // 可以指定范围,例如 [1,f.length-1)
        Arrays.parallelSort(f, 1, f.length - 1);
        System.out.println(Arrays.toString(f)); // [3, 1, 2, 34, 67, 76, 13]

        /*
         * Sorts the specified array into ascending numerical order.
         * 将指定数组按升序排列。
         * */
        int[] g = {34, 12, 54, 21, 23, 75};
        Arrays.sort(g);
        System.out.println(Arrays.toString(g));

        /*
         * Set all elements of the specified array, 
         * in parallel, using the provided generator function to compute each element.
         * 
         * 8 使用提供的生成器函数并行地设置指定数组的所有元素,以计算每个元素。
         * */
        Arrays.parallelSetAll(e, x -> x + 15);
        System.out.println(Arrays.toString(e)); // [15, 16, 17, 18, 19]

        /*
         * Set all elements of the specified array, 
         * using the provided generator function to compute each element.
         * 使用提供的生成器函数来计算每个元素,设置指定数组的所有元素。
         * 上面是并行的
         * */
        Arrays.setAll(e, x -> x + 1);
        System.out.println(Arrays.toString(e)); // [1, 2, 3, 4, 5]

        /*
         * Returns a sequential IntStream with the specified array as its source.
         * 
         * 9 返回指定数组作为源的序列IntStream。
         * */
        Arrays.stream(e).forEach(System.out::print); // 12345
        System.out.println();

        Arrays.stream(e).map(x -> x + 2).filter(x -> x < 6).forEach(System.out::print); 
        // 345
        System.out.println();

        System.out.println(Arrays.toString(e)); // [1, 2, 3, 4, 5]

        int[] h = Arrays.stream(e).map(x -> x + 5).toArray();
        System.out.println(Arrays.toString(h)); // [6, 7, 8, 9, 10]

        /*
         * Performs a reduction on the elements of this stream, 
         * using an associative accumulation function,
         * and returns an OptionalInt describing the reduced value, if any.
         * 
         * 10 使用关联累加函数对这个流的元素执行一个约简,并返回一个OptionalInt,
         * 描述约简后的值(如果有的话)。
         * */
        String res = Arrays.stream(e).reduce((x, y) -> x + y).toString();
        System.out.println(res); // OptionalInt[15]

        int res1 = Arrays.stream(e).reduce((x, y) -> x + y).getAsInt();
        System.out.println(res1); // 15

        System.out.println(Arrays.toString(e)); // [1, 2, 3, 4, 5]
        System.out.println(Arrays.stream(e).reduce((x, y) -> x + 1).getAsInt()); 
        // 5
        System.out.println(Arrays.stream(e).reduce((x, y) -> x + y + 1).getAsInt()); 
        // 19

        /*
         * Performs a reduction on the elements of this stream,
         * using the provided identity value and an associative accumulation function,
         * and returns the reduced value.
         * 使用提供的标识值和关联累积函数对该流的元素执行约简,并返回约简后的值。
         * */
        System.out.println(Arrays.stream(e).reduce(1, (x, y) -> x + y)); // 16(=15+1)
        System.out.println(Arrays.stream(e).reduce(4, (x, y) -> x + y)); // 19(=15+4)

    }
}

 

posted @ 2021-01-25 18:05  快乐的张小凡  阅读(115)  评论(0编辑  收藏  举报