Arrays类

数组的工具类java.util.Arrays,是java.util包中的类,在我们的代码中想使用这个类的话,就必须使用import进行导入。

import java.util.Arrays;

Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接使用类名进行调用,而"不用"使用对象来调用(注意:是"不用" 而不是 "不能")

Arrays类中方法调用

toString()方法

把数组转换为字符串形式并返回

修饰语和类型方法和说明
static StringtoString(boolean[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(byte[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(char[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(double[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(float[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(int[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(long[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(Object[] a)
返回指定数组的内容的字符串表示形式。
static StringtoString(short[] a)
返回指定数组的内容的字符串表示形式。
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    String strarr = Arrays.toString(arr);
    System.out.println(strarr);
  }
}

QQ截图20190425130827.jpg

binarySearch()方法

在数组中使用二分搜索法来搜索指定的数组,以获得指定的值。必须在进行此调用之前对数组进行排序(通过sort方法等)。如果没有对数组进行排序,则结果是不确定的。如果数组包含多个带有指定值的元素,则无法保证找到的是哪一个。

修饰语和类型方法和说明
static intbinarySearch(byte[] a,byte key)
使用二进制搜索算法搜索指定值的指定字节数组。
static intbinarySearch(byte[] a,int fromIndex,int toIndex,byte key)
使用二进制搜索算法搜索指定值的指定字节数组的范围。
static intbinarySearch(char[] a,char key)
使用二进制搜索算法搜索指定数组的指定值。
static intbinarySearch(char[] a,int fromIndex,int toIndex,char key)
使用二分搜索算法搜索指定值的指定数组的范围。
static intbinarySearch(double[] a,double key)
使用二进制搜索算法搜索指定值的指定数组的双精度值。
static intbinarySearch(double[] a,int fromIndex,int toIndex,double key)
使用二分搜索算法搜索指定值的指定数组的双精度范围。
static intbinarySearch(float[] a,float key)
使用二叉搜索算法搜索指定数组的浮点数。
static intbinarySearch(float[] a,int fromIndex,int toIndex,float key)
使用二分搜索算法搜索指定数组的浮点数范围。
static intbinarySearch(int[] a,int key)
使用二叉搜索算法搜索指定的int数组的指定值。
static intbinarySearch(int[] a,int fromIndex,int toIndex,int key)
使用二叉搜索算法搜索指定值的指定数组的范围。
static intbinarySearch(long[] a,int fromIndex,int toIndex,long key)
使用二分搜索算法搜索指定值的指定数组的范围。
static intbinarySearch(long[] a,long key)
使用二进制搜索算法搜索指定数组的指定数组。
static intbinarySearch(Object[] a,int fromIndex,int toIndex,Object key)
使用二进制搜索算法搜索指定对象的指定数组的范围。
static intbinarySearch(Object[] a,Object key)
使用二叉搜索算法搜索指定对象的指定数组。
static intbinarySearch(short[] a,int fromIndex,int toIndex,short key)
使用二进制搜索算法搜索指定值的指定数组的短整型范围。
static intbinarySearch(short[] a,short key)
使用二进制搜索算法搜索指定值的指定数组的指定值。
static intbinarySearch(T[] a,int fromIndex,int toIndex,T key,Comparator c)
使用二进制搜索算法搜索指定对象的指定数组的范围。
static intbinarySearch(T[] a,T key,Comparator c)
使用二叉搜索算法搜索指定对象的指定数组。
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    Arrays.sort(arr);
    int res = Arrays.binarySearch(arr,1);
    System.out.println(res);
  }
}

QQ截图20190425130800.jpg

sort()方法

把数据中的元素进行排序

修饰语和类型方法和说明
static voidsort(byte[] a)
按照数字顺序排列指定的数组。
static voidsort(byte[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(char[] a)
按照数字顺序排列指定的数组。
static voidsort(char[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(double[] a)
按照数字顺序排列指定的数组。
static voidsort(double[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(float[] a)
按照数字顺序排列指定的数组。
static voidsort(float[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(int[] a)
按照数字顺序排列指定的数组。
static voidsort(int[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(long[] a)
按照数字顺序排列指定的数组。
static voidsort(long[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(Object[] a)
对指定对象升序排列的阵列,根据natural ordering的元素。
static voidsort(Object[] a,int fromIndex,int toIndex)
对指定对象升序排列的数组的指定范围内,根据natural ordering的元素。
static voidsort(short[] a)
按照数字顺序排列指定的数组。
static voidsort(short[] a,int fromIndex,int toIndex)
按升序排列数组的指定范围。
static voidsort(T[] a,Comparator c)
根据指定的比较器引发的顺序对指定的对象数组进行排序。
static voidsort(T[] a,int fromIndex,int toIndex,Comparator c)
根据指定的比较器引发的顺序对指定的对象数组的指定范围进行排序。

对数组按数字升序进行排序

import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    Arrays.sort(arr);
    String strarr = Arrays.toString(arr);
    System.out.println(strarr);
  }
}

QQ截图20190425131122.jpg

对数组的指定范围按数字升序进行排序

import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    Arrays.sort(arr,0,3);
    String strarr = Arrays.toString(arr);
    System.out.println(strarr);
  }
}

QQ截图20190425131302.jpg

copyOf()方法

复制或者截取指定数组并返回

修饰语和类型方法和说明
static boolean[]copyOf(boolean[] original,int newLength)
使用false(如有必要)复制指定的数组,截断或填充,以使副本具有指定的长度。
static byte[]copyOf(byte[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static char[]copyOf(char[] original,int newLength)
复制指定的数组,截断或填充空字符(如有必要),以便复制具有指定的长度。
static double[]copyOf(double[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static float[]copyOf(float[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static int[]copyOf(int[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static long[]copyOf(long[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static short[]copyOf(short[] original,int newLength)
复制指定的数组,用零截取或填充(如有必要),以便复制具有指定的长度。
static T[]copyOf(T[] original,int newLength)
复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。
static T[]copyOf(U[] original,int newLength,类 newType)
复制指定的数组,用空值截断或填充(如有必要),以便复制具有指定的长度。
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    int[] copy = Arrays.copyOf(arr,8);
    String strarr = Arrays.toString(copy);
    System.out.println(strarr);
  }
}

QQ截图20190425131749.jpg

copyOfRange()方法

将数组中指定范围复制新数组并返回

修饰语和类型方法和说明
static boolean[]copyOfRange(boolean[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static byte[]copyOfRange(byte[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static char[]copyOfRange(char[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static double[]copyOfRange(double[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static float[]copyOfRange(float[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static int[]copyOfRange(int[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static long[]copyOfRange(long[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static short[]copyOfRange(short[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static T[]copyOfRange(T[] original,int from,int to)
将指定数组的指定范围复制到新数组中。
static T[]copyOfRange(U[] original,int from,int to,类 newType)
将指定数组的指定范围复制到新数组中。
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    int[] copy = Arrays.copyOfRange(arr,1,3);
    String strarr = Arrays.toString(copy);
    System.out.println(strarr);
  }
}

QQ截图20190425132034.jpg

equals()方法

比较俩个数组是否相等

修饰语和类型方法和说明
static booleanequals(boolean[] a,boolean[] a2)
如果两个指定的布尔数组彼此相等,则返回true
static booleanequals(byte[] a,byte[] a2)
如果两个指定的字节数组彼此相等,则返回true
static booleanequals(char[] a,char[] a2)
如果两个指定的字符数组彼此相等,则返回true
static booleanequals(double[] a,double[] a2)
如果两个指定的双精度数组彼此相等,则返回true
static booleanequals(float[] a,float[] a2)
如果两个指定的浮动数组彼此相等,则返回true
static booleanequals(int[] a,int[] a2)
如果两个指定的int数组彼此相等,则返回true
static booleanequals(long[] a,long[] a2)
如果两个指定的longs数组彼此相等,则返回true
static booleanequals(Object[] a,Object[] a2)
如果两个指定的对象数组彼此相等,则返回true
static booleanequals(short[] a,short[] a2)
如果两个指定的短裤阵列彼此相等,则返回true
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    int[] arr2 = {5,1,54,32,6};
    boolean res = Arrays.equals(arr,arr2);
    System.out.println(res);
    System.out.println(arr == arr2);
  }
}

注意:==比较的是引用所指向对象的内存地址 Arrays.equals方法比较是俩个数组中的内容

QQ截图20190425132317.jpg

fill()方法

用指定值去填充数组对象

修饰语和类型方法和说明
static voidfill(boolean[] a,boolean val)
将指定的布尔值分配给指定的布尔数组的每个元素。
static voidfill(boolean[] a,int fromIndex,int toIndex,boolean val)
将指定的布尔值分配给指定数组布尔值的指定范围的每个元素。
static voidfill(byte[] a,byte val)
将指定的字节值分配给指定字节数组的每个元素。
static voidfill(byte[] a,int fromIndex,int toIndex,byte val)
将指定的字节值分配给指定字节数组的指定范围的每个元素。
static voidfill(char[] a,char val)
将指定的char值分配给指定的char数组的每个元素。
static voidfill(char[] a,int fromIndex,int toIndex,char val)
将指定的char值分配给指定的char数组的指定范围的每个元素。
static voidfill(double[] a,double val)
将指定的double值分配给指定的双精度数组的每个元素。
static voidfill(double[] a,int fromIndex,int toIndex,double val)
将指定的double值分配给指定的双精度数组范围的每个元素。
static voidfill(float[] a,float val)
将指定的float值分配给指定的浮点数组的每个元素。
static voidfill(float[] a,int fromIndex,int toIndex,float val)
将指定的浮点值分配给指定的浮点数组的指定范围的每个元素。
static voidfill(int[] a,int val)
将指定的int值分配给指定的int数组的每个元素。
static voidfill(int[] a,int fromIndex,int toIndex,int val)
将指定的int值分配给指定的int数组的指定范围的每个元素。
static voidfill(long[] a,int fromIndex,int toIndex,long val)
将指定的long值分配给指定的longs数组的指定范围的每个元素。
static voidfill(long[] a,long val)
将指定的long值分配给指定的longs数组的每个元素。
static voidfill(Object[] a,int fromIndex,int toIndex,Object val)
将指定的对象引用分配给指定的对象数组的指定范围的每个元素。
static voidfill(Object[] a,Object val)
将指定的对象引用分配给指定的对象数组的每个元素。
static voidfill(short[] a,int fromIndex,int toIndex,short val)
将指定的短值分配给指定的短裤数组的指定范围的每个元素。
static voidfill(short[] a,short val)
将指定的短值分配给指定的短裤数组的每个元素。

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

import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    Arrays.fill(arr,1997);
    String strarr = Arrays.toString(arr);
        System.out.println(strarr);
  }
}

QQ截图20190425132814.jpg

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

import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
    Arrays.fill(arr,0,3,1997);
    String strarr = Arrays.toString(arr);
        System.out.println(strarr);
  }
}

QQ截图20190425132839.jpg

asList()方法

可以把数组转换为List集合

修饰语和类型方法和说明
static ListasList(T... a)
返回由指定数组支持的固定大小的列表。

hashCode()方法

基于指定数组的内容返回哈希码

修饰语和类型方法和说明
static inthashCode(boolean[] a)
根据指定数组的内容返回哈希码。
static inthashCode(byte[] a)
根据指定数组的内容返回哈希码。
static inthashCode(char[] a)
根据指定数组的内容返回哈希码。
static inthashCode(double[] a)
根据指定数组的内容返回哈希码。
static inthashCode(float[] a)
根据指定数组的内容返回哈希码。
static inthashCode(int[] a)
根据指定数组的内容返回哈希码。
static inthashCode(long[] a)
根据指定数组的内容返回哈希码。
static inthashCode(Object[] a)
根据指定数组的内容返回哈希码。
static inthashCode(short[] a)
根据指定数组的内容返回哈希码。
import java.util.Arrays;
public class arrays
{
  public static void main(String[] args)
  {
    int[] arr = {5,1,54,32,6};
        System.out.println(Arrays.hashCode(arr));
  }
}

QQ截图20190425133728.jpg