Arrays 的使用

2024-01-07 19:02:11

Arrays

概述

提供了数组操作的相关方法,连接数组和集合

asList

  1. 返回指定数组的列表
  2. 列表和数组的引用位置相同
      Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
        List<Integer> list = Arrays.asList(arrs);
        System.out.println(list);
        arrs[5] = 100;
        System.out.println(list);
        //[1, 2, 3, 4, 5, 6, 7, 8, 9]
        //[1, 2, 3, 4, 5, 100, 7, 8, 9]
    }

binarySearch

  1. 二分法查找元素
  2. 需要进行排序
Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
        
        int num = Arrays.binarySearch(arrs, 5);
        System.out.println(num);
//4

copyOf copyOfRange

  1. 创建数组副本
  2. 一个新的数组
     Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
        
        Integer[] arrs1 = Arrays.copyOf(arrs, 5);
        for(Integer i : arrs1) {
            System.out.print(i + " ");
        }
        System.out.println();
        //1 2 3 4 5 
        
        arrs1[3] = 100;
        for(Integer i : arrs) {
            System.out.print(i + " ");
        }
        //1 2 3 4 5 6 7 8 9 
        System.out.println();
        for(Integer i : arrs1) {
            System.out.print(i + " ");
        }
        //1 2 3 100 5 
         
        Integer[] ret = Arrays.copyOf(arrs,20,Integer[].class);
        
        System.out.println();
        for(Integer i : ret) {
            System.out.print(i + " ");
        }
        //1 2 3 4 5 6 7 8 9 null null null null null null null null null null null



   		Integer[] range = Arrays.copyOfRange(arrs, 5, 20);
        
        System.out.println();
        for(Integer i : ret) {
            System.out.print(i + " ");
        }
        //1 2 3 4 5 6 7 8 9 null null null null null null null null null null null 

equals deepEquals

  1. deepEquals 是深层次的比较,比如嵌套数组
        Integer[] arrs = new Integer[] {1,2,3,4,5,6,7,8,9};
        Integer[] arrs1 = new Integer[] {1,2,3,4,5,6,7,8,9};
        System.out.println(arrs);
        System.out.println(arrs1);
        System.out.println(Arrays.equals(arrs, arrs1));
//        [Ljava.lang.Integer;@2077d4de
//        [Ljava.lang.Integer;@7591083d
//        true

Arrays.fill填充数组

    public void test5() {
        Integer[] arrs = new Integer[10];
        Arrays.fill(arrs, 100);
        for(Integer i : arrs) {
            System.out.print(i + " ");
        }
        //100 100 100 100 100 100 100 100 100 100 
    }

sort

 public void test6() {
        Integer[] arrs = new Integer[20];
        for(int i=0;i<20;i++)
            arrs[i] = (int)(Math.random()*100);
        
        for(int i:arrs) {
            System.out.print(i + " ");
        }
        System.out.println();
        
        //49 40 0 79 4 98 12 81 71 27 9 45 16 17 7 92 63 22 73 22 
        
        Arrays.sort(arrs);
        
        for(int i:arrs) 
            System.out.print(i + " ");
        
        System.out.println();
        
        //0 4 7 9 12 16 17 22 22 27 40 45 49 63 71 73 79 81 92 98 
        
        Arrays.sort(arrs,(o1,o2)->o2-o1);
        
        for(int i:arrs) 
            System.out.print(i + " ");
        
        //98 92 81 79 73 71 63 49 45 40 27 22 22 17 16 12 9 7 4 0 
        
    }

parallelSort

  1. 经过比较 速断快与sort
  2. 使用频率越高越明显
  3. 数据量越大越明显从1000~10000000开始进行100次测试
  public void test7() {
        
        //生产1万个元素
        int len = 100000;
        Integer[] arrs = new Integer[len];
        for(int i=0;i<len;i++)
            arrs[i] = (int)(Math.random()*100);
        
        long t1 = System.currentTimeMillis();
        Arrays.sort(arrs);
        long t2 = System.currentTimeMillis();
        System.out.println("sort\t"+(t2-t1));
        
        for(int i=0;i<len;i++)
            arrs[i] = (int)(Math.random()*100);
        
        long t3 = System.currentTimeMillis();
        Arrays.parallelSort(arrs);
        long t4 = System.currentTimeMillis();
        System.out.println("parallelSort\t"+(t4-t3));
        System.out.println("------------------------------");
    }
 public void test8() {
        for(int i=0;i<100;i++) {
            test7();
        }
    }
sort	35
parallelSort	64
------------------------------
sort	40
parallelSort	73
------------------------------
sort	107
parallelSort	53
------------------------------
sort	73
parallelSort	63
------------------------------
sort	74
parallelSort	62
····
····

spliterator 遍历数组

  int len = 100;
        Integer[] arrs = new Integer[len];
        for(int i=0;i<len;i++)
            arrs[i] = (int)(Math.random()*100);
        
        Spliterator<Integer> s = Arrays.spliterator(arrs);
        
        s.forEachRemaining((e)->System.out.print(e + " "));

Stream

    public void test10() {
        int len = 10;
        Integer[] arrs = new Integer[len];
        for(int i=0;i<len;i++)
            arrs[i] = (int)(Math.random()*100);
        
        List<Integer> list = Arrays.stream(arrs).collect(Collectors.toList());
        System.out.println(list);
        //[56, 33, 59, 63, 55, 37, 68, 52, 53, 3]
    }

setAll parallelSetAll 为所有的元素赋值

  1. parallelSetAll 并行处理,速度会快
    public void test11() {
        int len = 10;
        Integer[] array = new Integer[len];
        
        
        Arrays.setAll(array, (e)->10);
        
        for(int i:array)
            System.out.print(i + " ");
        
        System.out.println();
        Arrays.parallelSetAll(array, (e)->e + 10);
        
        for(int i:array)
            System.out.print(i + " ");

//        10 10 10 10 10 10 10 10 10 10 
//        10 11 12 13 14 15 16 17 18 19 
    }

parallelPrefix 每个元素都包含对前面的所有元素应用某个操作的累计结果

    public void test12() {
        int len = 10;
        int[] array = new int[len];

        Arrays.parallelPrefix(array, (e,u)->{
            System.out.println(e + "\t" + u);
            return e +1;
        });
        System.out.println();
        for(int i:array)
            System.out.print(i + " ");
    }

toString deepToString(嵌套数组) hashCode deepHashCode

  public void test13() {
        int len = 10;
        Integer[] arrs = new Integer[len];
        for(int i=0;i<len;i++)
            arrs[i] = (int)(Math.random()*100);
        
        System.out.println(Arrays.toString(arrs));
        System.out.println(Arrays.hashCode(arrs));
    }

文章来源:https://blog.csdn.net/i_am_bad_man/article/details/135442144
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。