目录
  • java常用类库arrays
    • 1.1 tostring
    • 1.2 sort
      • 1.2.1 sort​(t[] a, int fromindex, int toindex)
      • 1.2.2 sort(t[] a)
    • 1.3 copyof
        • 1.5 binarysearch
          • 1.5.1 binarysearch​(t[] a, int fromindex, int toindex, t key)
          • 1.5.2 binarysearch​(t[] a, t key)
          • 1.6.1 equals​(t[] a, int afromindex, int atoindex, t[] b, int bfromindex, int btoindex)
          • 1.6.2 equals​(t[] a, t[] a2)
          • 1.7.1 fill​(t[] a, int fromindex, int toindex, t val)
          • 1.7.2 fill​(t[] a, t val)

          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 type field description
          static string deeptostring​(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 type field description
          static void sort​(t[] a, int fromindex, int toindex, comparator<? super t> c) 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
          static void sort​(t[] a, comparator<? super t> c) 根据指定比较器引发的顺序对指定的对象数组进行排序。
          static void parallelsort​(t[] a) 将指定的数组按升序排序。
          static void parallelsort​(t[] a, int fromindex, int toindex) 将指定的数组范围按数字升序排序。
          static <t extends comparable<? super t>>void parallelsort​(t[] a) 根据元素的natural ordering对指定的对象数组按升序排序。
          static <t extends comparable<? super t>>void parallelsort​(t[] a, int fromindex, int toindex) 根据元素的natural ordering ,将指定对象数组的指定范围按升序排序。
          static void parallelsort​(t[] a, int fromindex, int toindex, comparator<? super t> cmp) 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序。
          static void parallelsort​(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 type field description
          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 type field description
          static int mismatch​(t[] a, int afromindex, int atoindex, t[] b, int bfromindex, int btoindex, comparator<? super t> cmp) 查找并返回指定范围内两个 object数组之间第一个不匹配的相对索引,否则如果未找到不匹配则返回-1。
          static int mismatch​(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 type field description
          static int binarysearch​(t[] a, int fromindex, int toindex, t key, comparator<? super t> c) 使用二进制搜索算法搜索指定对象的指定数组范围
          static int binarysearch​(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 type field description
          static boolean deepequals​(object[] a1, object[] a2) 如果两个指定的数组彼此 深度相等 ,则返回 true
          static boolean equals​(t[] a, int afromindex, int atoindex, t[] b, int bfromindex, int btoindex, comparator<? super t> cmp) 如果在指定范围内指定的两个object数组彼此 相等 ,则返回true
          static boolean equals​(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 type field description
          static list aslist​(t… a) 返回由指定数组支持的固定大小的列表。
          static int compare​(t[] a, t[] b) 字典顺序比较两个t阵列
          static int compare​(t[] a, int afromindex, int atoindex,t[] b, int bfromindex, int btoindex) 在指定范围内按字典顺序比较两个t阵列
          static <t extends comparable<? super t>>int compare​(t[] a, int afromindex, int atoindex, t[] b, int bfromindex, int btoindex) 在指定范围内按字典顺序比较两个 object阵列。
          static int compare​(t[] a, int afromindex, int atoindex, t[] b, int bfromindex, int btoindex, comparator<? super t> cmp) 在指定范围内按字典顺序比较两个 object阵列。
          static <t extends comparable<? super t>>int compare​(t[] a, t[] b) 按 object顺序比较两个 object阵列,在可比元素中。
          static int compare​(t[] a, t[] b, comparator<? super t> cmp) 使用指定的比较器按字典顺序比较两个 object阵列
          static int compareunsigned​(t[] a, t[] b) byte字典顺序比较两个t阵列,数字处理元素为无符号
          static int compareunsigned​(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 int hashcode​(t[] a) 根据指定数组的内容返回哈希码
          static int deephashcode​(object[] a) 返回基于指定数组的“深层内容”的哈希码
          static void parallelprefix​(t[] array, int fromindex, int toindex,tbinaryoperator op) 对于给定的数组子范围执行parallelprefix(t[], tbinaryoperator)
          static void parallelprefix​(dt[] array, tbinaryoperator op) 使用提供的函数并行地累积给定数组的每个元素
          static void parallelprefix​(t[] array, int fromindex, int toindex, binaryoperator op) 对于给定的数组子范围执行 parallelprefix(object[], binaryoperator)
          static void parallelprefix​(t[] array, binaryoperator op) 使用提供的函数并行地累积给定数组的每个元素
          static void parallelsetall​(double[] array, inttodoublefunction generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
          static void parallelsetall​(int[] array, intunaryoperator generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
          static void parallelsetall​(long[] array, inttolongfunction generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
          static void parallelsetall​(t[] array, intfunction<? extends t> generator) 使用提供的生成器函数并行设置指定数组的所有元素以计算每个元素
          static void setall​(double[] array, inttodoublefunction generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
          static void setall​(int[] array, intunaryoperator generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
          static void setall​(long[] array, inttolongfunction generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
          static void setall​(t[] array, intfunction<? extends t> generator) 使用提供的生成器函数设置指定数组的所有元素以计算每个元素
          static spliterator.ofdouble spliterator​(double[] array) 返回覆盖所有指定数组的spliterator.ofdouble
          static spliterator.ofdouble spliterator​(double[] array, int startinclusive, int endexclusive) 返回覆盖指定数组的指定范围的spliterator.ofdouble
          static spliterator.ofint spliterator​(int[] array) 返回覆盖所有指定数组的spliterator.ofint
          static spliterator.ofint spliterator​(int[] array, int startinclusive, int endexclusive) 返回覆盖指定数组的指定范围的spliterator.ofint
          static spliterator.oflong spliterator​(long[] array) 返回覆盖所有指定数组的spliterator.oflong
          static spliterator.oflong spliterator​(long[] array, int startinclusive, int endexclusive) 返回覆盖指定数组的指定范围的spliterator.oflong
          static spliterator spliterator​(t[] array) 返回覆盖所有指定数组的spliterator
          static spliterator spliterator​(t[] array, int startinclusive, int endexclusive) 返回覆盖指定数组的指定范围的spliterator
          static doublestream stream​(double[] array) 返回以指定数组作为源的顺序doublestream
          static doublestream stream​(double[] array, int startinclusive, int endexclusive) 返回指定数组的指定范围作为其源的顺序doublestream
          static intstream stream​(int[] array) 返回以指定数组作为源的顺序intstream
          static intstream stream​(int[] array, int startinclusive, int endexclusive) 返回指定数组的指定范围作为其源的顺序intstream
          static longstream stream​(long[] array) 返回以指定数组作为源的顺序longstream
          static longstream stream​(long[] array, int startinclusive, int endexclusive) 返回指定数组的指定范围作为其源的顺序longstream
          static stream stream​(t[] array) 返回以指定数组作为源的顺序stream
          static stream stream​(t[] array, int startinclusive, int endexclusive) 返回指定数组的指定范围作为其源的顺序stream

          总结

          本篇文章就到这里了,希望能给你带来帮助,也希望您能够多多关注www.887551.com的更多内容!