Java Arrays工具类的使用

2022年6月9日08:07:23

代码有点小多,耐心看。。。

import java.util.Arrays;import java.util.List;/**
 * fileName: ArrayTester
 * description: 使用Arrays工具类
 *
 * @author lihaogn-main
 * @version 1.0
 * @date 2019/9/10 20:14
 */publicclassArrayTester{publicstaticvoidmain(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 二分搜索,返回下标,没有找到返回-1int[] 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=newint[5];
        Arrays.fill(d,5);
        System.out.println(Arrays.toString(d));// [5, 5, 5, 5, 5]// 填充数组e下标[1,3)的值int[] e=newint[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)}}
  • 作者:豪华手抓饼
  • 原文链接:https://blog.csdn.net/lihaogn/article/details/100731306
    更新时间:2022年6月9日08:07:23 ,共 3642 字。