Java Arrays工具类的使用

代码有点小多,耐心看。。。
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)

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值