Java核心类库(一)——常用类库(Arrays)

Java常用类库Arrays

类Arrays包含用于操作数组的各种方法(例如排序和搜索)

  • 如果指定的数组引用为null,则此类中的方法都抛出NullPointerException ,除非另有说明

一、常用方法

1.1 toString

返回指定数组内容的字符串形式

举例
       int[] a1 = {1,2,3,4,5};
       System.out.println(Arrays.toString(a1));//[1, 2, 3, 4, 5]
源码
	public static String toString(int[] a) {
        if (a == null)
            return "null";
        int iMax = a.length - 1;
        if (iMax == -1)
            return "[]";

        StringBuilder b = new StringBuilder();
        b.append('[');
        for (int i = 0; ; i++) {
            b.append(a[i]);
            if (i == iMax)
                return b.append(']').toString();
            b.append(", ");
        }
    }
其它
Modifier and TypeFieldDescription
static StringdeepToString​(Object[] a)返回指定数组的“深层内容”的字符串表示形式

1.2 Sort

排序(默认升序)

1.2.1 sort​(T[] a, int fromIndex, int toIndex)

指定区间进行排序

举例
       int[] a1 = {9,1,3,7,2,5};
       System.out.println(Arrays.toString(a1));//[9, 1, 3, 7, 2, 5]
       Arrays.sort(a1,0,3);//[0,3),对9,1,3进行排序
       System.out.println(Arrays.toString(a1));//[0[1, 3, 9, 7, 2, 5]
源码
    public static void sort(int[] a, int fromIndex, int toIndex) {
        rangeCheck(a.length, fromIndex, toIndex);
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
    }
1.2.2 Sort(T[] a)

对整个数组进行排序

举例
       int[] a1 = {0,7,8,2,4,1};
       System.out.println(Arrays.toString(a1));//[0, 7, 8, 2, 4, 1]
       Arrays.sort(a1);
       System.out.println(Arrays.toString(a1));//[0, 1, 2, 4, 7, 8]
源码
    public static void sort(int[] a) { DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
    }
    
1.2.3 其它
Modifier and TypeFieldDescription
static voidsort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
static voidsort​(T[] a, Comparator<? super T> c)根据指定比较器引发的顺序对指定的对象数组进行排序。
static voidparallelSort​(T[] a)将指定的数组按升序排序。
static voidparallelSort​(T[] a, int fromIndex, int toIndex)将指定的数组范围按数字升序排序。
static <T extends Comparable<? super T>>voidparallelSort​(T[] a)根据元素的natural ordering对指定的对象数组按升序排序。
static <T extends Comparable<? super T>>voidparallelSort​(T[] a, int fromIndex, int toIndex)根据元素的natural ordering ,将指定对象数组的指定范围按升序排序。
static voidparallelSort​(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp)根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
static voidparallelSort​(T[] a, Comparator<? super T> cmp)根据指定比较器引发的顺序对指定的对象数组进行排序。

1.3 copyOf

复制(常用于数组扩容)

举例
       int[] a = {1,2,3};
       System.out.println(a.length);//output:3
       a = Arrays.copyOf(a,15);
       System.out.println(a.length);//output:15
源码
  public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }
其它
Modifier and TypeFieldDescription
static T[]copyOf​(T[] original, int newLength)使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度
static <T,​U>T[]copyOf​(U[] original, int newLength, 类<? extends T[]> newType)使用空值复制指定的数组,截断或填充(如有必要),以使副本具有指定的长度

1.4 mismatch

举例
       int[] a1 = {0,1,2,3,4,5};
       int[] a2 = {0,1,2,3,4,5};//与a1相同
       int[] a3 = {0,1,2,3,0,5};//从索引4开始与a1不同
       System.out.println(Arrays.mismatch(a1,a2));//output:-1
       System.out.println(Arrays.mismatch(a1,a3));//output:4
源码
 public static int mismatch(int[] a, int[] b) {
        int length = Math.min(a.length, b.length); // Check null array refs
        if (a == b)
            return -1;

        int i = ArraysSupport.mismatch(a, b, length);
        return (i < 0 && a.length != b.length) ? length : i;
    }
其它
Modifier and TypeFieldDescription
static intmismatch​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)查找并返回指定范围内两个 Object数组之间第一个不匹配的相对索引,否则如果未找到不匹配则返回-1。
static intmismatch​(T[] a, T[] b, Comparator<? super T> cmp)查找并返回两个 Object数组之间第一个不匹配的索引,否则如果未找到不匹配则返回-1。

1.5 binarySearch

二分查找,搜索,返回下标

1.5.1 binarySearch​(T[] a, int fromIndex, int toIndex, T key)

限定了搜索的范围[fromIndex, toIndex)

举例
  int[] a = {1,2,3,4,5};
       int x1 = Arrays.binarySearch(a,2,3,4);//在a数组下标[2,3)中查找值为4的下标
       System.out.println(x1);//output:<0的随机数
源码
	public static int binarySearch(int[] a, int fromIndex, int toIndex,int key) {
        rangeCheck(a.length, fromIndex, toIndex);
        return binarySearch0(a, fromIndex, toIndex, key);
    }
    
    private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }
1.5.2 binarySearch​(T[] a, T key)

与上述相同,只是没有限定范围,fromIndex=0, toIndex=length

举例
       int[] a = {1,2,3,4,5};
       int x1 = Arrays.binarySearch(a,3);//在a数组中查找值为3的下标
       int x2 = Arrays.binarySearch(a,-6);//在a数组中查找值为6的下标
       System.out.println(x1);//output:2
       System.out.println(x2);//output:<0的随机数
源码
    public static int binarySearch(int[] a, int key) {
        return binarySearch0(a, 0, a.length, key);
    }
    
	private static int binarySearch0(int[] a, int fromIndex, int toIndex,int key) {
        int low = fromIndex;
        int high = toIndex - 1;

        while (low <= high) {
            int mid = (low + high) >>> 1;
            int midVal = a[mid];

            if (midVal < key)
                low = mid + 1;
            else if (midVal > key)
                high = mid - 1;
            else
                return mid; // key found
        }
        return -(low + 1);  // key not found.
    }
1.5.3 其它
Modifier and TypeFieldDescription
static intbinarySearch​(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)使用二进制搜索算法搜索指定对象的指定数组范围
static intbinarySearch​(T[] a, T key, Comparator<? super T> c)使用二进制搜索算法在指定的数组中搜索指定的对象

1.6 equals

1.6.1 equals​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)

如果两个指定数组在指定范围内相等 ,则返回true

举例
       int[] a1 = {1,2,3,4,5};
       int[] a2 = {1,2,0,0,4,5};
       System.out.println(Arrays.equals(a1,0,2,a2,0,2));//true
       System.out.println(Arrays.equals(a1,3,5,a2,4,6));//true
源码
public static boolean equals(int[] a, int aFromIndex, int aToIndex,int[] b, int bFromIndex, int bToIndex) {
        rangeCheck(a.length, aFromIndex, aToIndex);
        rangeCheck(b.length, bFromIndex, bToIndex);

        int aLength = aToIndex - aFromIndex;
        int bLength = bToIndex - bFromIndex;
        if (aLength != bLength)
            return false;

        return ArraysSupport.mismatch(a, aFromIndex, b, bFromIndex,aLength) < 0;
    }

1.6.2 equals​(T[] a, T[] a2)

如果两个指定数组相等,则返回 true

举例
       int[] a1 = {1,2,3,4,5};
       int[] a2 = {1,2,3,4,5};
       int[] a3 = {1,2,0,4,5};
       System.out.println(Arrays.equals(a1,a2));//true
       System.out.println(Arrays.equals(a1,a3));//false
源码
 public static boolean equals(int[] a, int[] a2) {
        if (a==a2)
            return true;
        if (a==null || a2==null)
            return false;

        int length = a.length;
        if (a2.length != length)
            return false;

        return ArraysSupport.mismatch(a, a2, length) < 0;
    }
1.6.3 其它
Modifier and TypeFieldDescription
static booleandeepEquals​(Object[] a1, Object[] a2)如果两个指定的数组彼此 深度相等 ,则返回 true
static booleanequals​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)如果在指定范围内指定的两个Object数组彼此 相等 ,则返回true
static booleanequals​(T[] a, T[] a2, Comparator<? super T> cmp)如果两个指定的Objects数组彼此 相等 ,则返回 true

1.7 fills

1.7.1 fill​(T[] a, int fromIndex, int toIndex, T val)

将指定的T值分配给指定的T类型数组的指定范围的每个元素

举例
       int[] a1 = new int[10];
       Arrays.fill(a1,1,4,8);
       char[] a2 = new char[10];
       Arrays.fill(a2,0,3,'s');
       System.out.println(Arrays.toString(a1));//[0, 8, 8, 8, 0, 0, 0, 0, 0, 0]
       System.out.println(Arrays.toString(a2));//[s, s, s,  ,  ,  ,  ,  ,  ,  ]
源码
    public static void fill(char[] a, int fromIndex, int toIndex, char val) {
        rangeCheck(a.length, fromIndex, toIndex);
        for (int i = fromIndex; i < toIndex; i++)
            a[i] = val;
    }
1.7.2 fill​(T[] a, T val)

将指定的T值分配给指定的T类型数组的每个元素

举例
       int[] a1 = new int[10];
       Arrays.fill(a1,8);
       char[] a2 = new char[10];
       Arrays.fill(a2,'s');
       System.out.println(Arrays.toString(a1));//[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]
       System.out.println(Arrays.toString(a2));//[s, s, s, s, s, s, s, s, s, s]
源码
	public static void fill(int[] a, int val) {
        for (int i = 0, len = a.length; i < len; i++)
            a[i] = val;
    }

二、其他方法

Modifier and TypeFieldDescription
static ListasList​(T… a)返回由指定数组支持的固定大小的列表。
static intcompare​(T[] a, T[] b)字典顺序比较两个T阵列
static intcompare​(T[] a, int aFromIndex, int aToIndex,T[] b, int bFromIndex, int bToIndex)在指定范围内按字典顺序比较两个T阵列
static <T extends Comparable<? super T>>intcompare​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)在指定范围内按字典顺序比较两个 Object阵列。
static intcompare​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)在指定范围内按字典顺序比较两个 Object阵列。
static <T extends Comparable<? super T>>intcompare​(T[] a, T[] b)按 Object顺序比较两个 Object阵列,在可比元素中。
static intcompare​(T[] a, T[] b, Comparator<? super T> cmp)使用指定的比较器按字典顺序比较两个 Object阵列
static intcompareUnsigned​(T[] a, T[] b)byte字典顺序比较两个T阵列,数字处理元素为无符号
static intcompareUnsigned​(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex)在指定范围内按字典顺序比较两个 T阵列,将元素数字处理为无符号
static T[]copyOfRange​(T[] original, int from, int to)将指定数组的指定范围复制到新数组中
static T[]copyOfRange​(T[] original, int from, int to)将指定数组的指定范围复制到新数组中
static <T,​U>T[]copyOfRange​(U[] original, int from, int to, 类<? extends T[]> newType)将指定数组的指定范围复制到新数组中
static inthashCode​(T[] a)根据指定数组的内容返回哈希码
static intdeepHashCode​(Object[] a)返回基于指定数组的“深层内容”的哈希码
static voidparallelPrefix​(T[] array, int fromIndex, int toIndex,TBinaryOperator op)对于给定的数组子范围执行parallelPrefix(T[], TBinaryOperator)
static voidparallelPrefix​(dT[] array, TBinaryOperator op)使用提供的函数并行地累积给定数组的每个元素
static voidparallelPrefix​(T[] array, int fromIndex, int toIndex, BinaryOperator op)对于给定的数组子范围执行 parallelPrefix(Object[], BinaryOperator)
static voidparallelPrefix​(T[] array, BinaryOperator op)使用提供的函数并行地累积给定数组的每个元素
static voidparallelSetAll​(double[] array, IntToDoubleFunction generator)使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static voidparallelSetAll​(int[] array, IntUnaryOperator generator)使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static voidparallelSetAll​(long[] array, IntToLongFunction generator)使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static voidparallelSetAll​(T[] array, IntFunction<? extends T> generator)使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
static voidsetAll​(double[] array, IntToDoubleFunction generator)使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static voidsetAll​(int[] array, IntUnaryOperator generator)使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static voidsetAll​(long[] array, IntToLongFunction generator)使用提供的生成器函数设置指定数组的所有元素以计算每个元素
static voidsetAll​(T[] array, IntFunction<? extends T> generator)使用提供的生成器函数设置指定数组的所有元素以计算每个元素
staticSpliterator.OfDouble spliterator​(double[] array)返回覆盖所有指定数组的Spliterator.OfDouble
staticSpliterator.OfDouble spliterator​(double[] array, int startInclusive, int endExclusive)返回覆盖指定数组的指定范围的Spliterator.OfDouble
staticSpliterator.OfInt spliterator​(int[] array)返回覆盖所有指定数组的Spliterator.OfInt
staticSpliterator.OfInt spliterator​(int[] array, int startInclusive, int endExclusive)返回覆盖指定数组的指定范围的Spliterator.OfInt
staticSpliterator.OfLong spliterator​(long[] array)返回覆盖所有指定数组的Spliterator.OfLong
staticSpliterator.OfLong spliterator​(long[] array, int startInclusive, int endExclusive)返回覆盖指定数组的指定范围的Spliterator.OfLong
static Spliteratorspliterator​(T[] array)返回覆盖所有指定数组的Spliterator
static Spliteratorspliterator​(T[] array, int startInclusive, int endExclusive)返回覆盖指定数组的指定范围的Spliterator
static DoubleStreamstream​(double[] array)返回以指定数组作为源的顺序DoubleStream
static DoubleStreamstream​(double[] array, int startInclusive, int endExclusive)返回指定数组的指定范围作为其源的顺序DoubleStream
static IntStreamstream​(int[] array)返回以指定数组作为源的顺序IntStream
static IntStreamstream​(int[] array, int startInclusive, int endExclusive)返回指定数组的指定范围作为其源的顺序IntStream
static LongStreamstream​(long[] array)返回以指定数组作为源的顺序LongStream
static LongStreamstream​(long[] array, int startInclusive, int endExclusive)返回指定数组的指定范围作为其源的顺序LongStream
static Streamstream​(T[] array)返回以指定数组作为源的顺序Stream
static Streamstream​(T[] array, int startInclusive, int endExclusive)返回指定数组的指定范围作为其源的顺序Stream
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Selcouther

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值