java.util.Arrays类使用简介

java.util.Arrays类包含用来操作数组的各种方法,比如:排序、搜索。此类还包含一个允许将数组作为列表来查看的静态工厂。
注意:Arrays类中的所有方法都是静态static的,所以调用的时候直接使用类名调用即可。

1.将数组转换为列表
public static <T> List<T> asList(T... a)
返回一个受指定数组支持的固定大小的列表。对返回列表的更改会“直接写”到数组,同样对数组的更改也会“直接写”到列表。

举例:
List<String> list = Arrays.asList("Red", "Green", "Blue");

String[] array = {"Red", "Green", "Blue"};
List<String> list = Arrays.asList(array);

2.二分法查找元素
public static int binarySearch(byte[] a, byte key)
public static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key)
public static int binarySearch(char[] a, char key)
public static int binarySearch(char[] a, int fromIndex, int toIndex, char key)
public static int binarySearch(short[] a, short key)
public static int binarySearch(short[] a, int fromIndex, int toIndex, short key)
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
public static int binarySearch(long[] a, long key)
public static int binarySearch(long[] a, int fromIndex, int toIndex, long key)
public static int binarySearch(float[] a, float key)
public static int binarySearch(float[] a, int fromIndex, int toIndex, float key)
public static int binarySearch(double[] a, double key)
public static int binarySearch(double[] a, int fromIndex, int toIndex, double key)
public static int binarySearch(Object[] a, Object key)
public static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key)
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)
public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c)
使用二分搜索法来搜索指定数组的范围,以获得指定对象。在进行此调用之前,必须根据指定的比较器对数组或范围进行升序排序。如果没有对数组或范围进行排序,则结果是不确定的。如果范围包含多个等于指定对象的元素,则无法保证找到的是哪一个。

举例:
String[] array = {"Alpha", "Alpha", "Black", "Blue", "Green", "Red", "White"};
Arrays.binarySearch(array, "Blue"); // 返回3
Arrays.binarySearch(array, "Brown"); // 返回-5(返回负值说明列表中没有找到该元素)
Arrays.binarySearch(array, "Alpha"); // 返回1

3.复制数组
public static boolean[] copyOf(boolean[] original, int newLength)
public static byte[] copyOf(byte[] original, int newLength)
public static char[] copyOf(char[] original, int newLength)
public static short[] copyOf(short[] original, int newLength)
public static int[] copyOf(int[] original, int newLength)
public static long[] copyOf(long[] original, int newLength)
public static float[] copyOf(float[] original, int newLength)
public static double[] copyOf(double[] original, int newLength)
public static <T> T[] copyOf(T[] original, int newLength)
public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)
将指定数组的元素复制到一个新数组。newLength可以超过数组索引范围。对于超过范围的索引,新数组将填充:false、0或null(根据数组元素类型而定)。

举例:
String[] array = {"Red", "Green", "Blue"};
String[] array1 = Arrays.copyOf(array, 2); // 返回{"Red", "Green"}
String[] array2 = Arrays.copyOf(array, 3); // 返回{"Red", "Green", "Blue"}
String[] array3 = Arrays.copyOf(array, 4); // 返回{"Red", "Green", "Blue", null}

4.复制数组的指定范围
public static boolean[] copyOfRange(boolean[] original, int from, int to)
public static byte[] copyOfRange(byte[] original, int from, int to)
public static char[] copyOfRange(char[] original, int from, int to)
public static short[] copyOfRange(short[] original, int from, int to)
public static int[] copyOfRange(int[] original, int from, int to)
public static long[] copyOfRange(long[] original, int from, int to)
public static float[] copyOfRange(float[] original, int from, int to)
public static double[] copyOfRange(double[] original, int from, int to)
public static <T> T[] copyOfRange(T[] original, int from, int to)
public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType)
将指定数组的指定范围复制到一个新数组。要复制的元素范围为从from到to-1索引处的元素。to可以超过数组索引范围。对于超过范围的索引,新数组将填充:false、0或null(根据数组元素类型而定)。

举例:
String[] array = {"Red", "Green", "Blue"};
String[] array1 = Arrays.copyOfRange(array, 1, 1); // 返回空数组
String[] array2 = Arrays.copyOfRange(array, 0, 1); // 返回{"Red"}
String[] array3 = Arrays.copyOfRange(array, 1, 3); // 返回{"Green", "Blue"}
String[] array4 = Arrays.copyOfRange(array, 1, 4); // 返回{"Green", "Blue", null}

5.判断两个数组是否相等
public static boolean equals(boolean[] a, boolean[] a2)
public static boolean equals(byte[] a, byte[] a2)
public static boolean equals(char[] a, char[] a2)
public static boolean equals(short[] a, short[] a2)
public static boolean equals(int[] a, int[] a2)
public static boolean equals(long[] a, long[] a2)
public static boolean equals(float[] a, float[] a2)
public static boolean equals(double[] a, double[] a2)
public static boolean equals(Object[] a, Object[] a2)
如果两个指定的数组彼此相等,则返回 true。如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都是相等的,则认为这两个数组是相等的。

举例:
String[] array1 = {"Red", "Green", "Blue"};
String[] array2 = {"Red", "Green", "Blue"};
String[] array3 = {"Red", "Blue", "Green"};
String[] array4 = {"Red", "Green", "Blue", "Black"};
Arrays.equals(array1, array2); // 返回true
Arrays.equals(array1, array3); // 返回false
Arrays.equals(array1, array4); // 返回false

6.以指定值填充数组的所有元素
public static void fill(boolean[] a, boolean val)
public static void fill(byte[] a, byte val)
public static void fill(char[] a, char val)
public static void fill(short[] a, short val)
public static void fill(int[] a, int val)
public static void fill(long[] a, long val)
public static void fill(float[] a, float val)
public static void fill(double[] a, double val)
public static void fill(Object[] a, Object val)
将指定的值赋值给指定数组的每个元素。

举例:
String[] array = {"Red", "Green", "Blue"};
Arrays.fill(array, "Black"); // 数组结果为{"Black", "Black", "Black"}

7.以指定值填充数组的指定元素
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
public static void fill(char[] a, int fromIndex, int toIndex, char val)
public static void fill(short[] a, int fromIndex, int toIndex, short val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static void fill(long[] a, int fromIndex, int toIndex, long val)
public static void fill(float[] a, int fromIndex, int toIndex, float val)
public static void fill(double[] a, int fromIndex, int toIndex, double val)
public static void fill(Object[] a, int fromIndex, int toIndex, Object val)
将指定的值赋值给指定数组的指定元素。要复制的元素范围为从from到to-1索引处的元素。如果to-1超出索引范围则抛出异常。

举例:
String[] array = {"Red", "Green", "Blue"};
Arrays.fill(array, 1, 1, "Black"); // 数组结果为{"Red", "Green", "Blue"}
Arrays.fill(array, 0, 1, "Black"); // 数组结果为{"Black", "Green", "Blue"}
Arrays.fill(array, 1, 3, "Black"); // 数组结果为{"Black", "Black", "Black"}
Arrays.fill(array, 1, 4, "Black"); // 因为4超过数组大小,所以抛出异常

8.返回数组的哈希码
public static int hashCode(boolean[] a)
public static int hashCode(byte[] a)
public static int hashCode(char[] a)
public static int hashCode(short[] a)
public static int hashCode(int[] a)
public static int hashCode(long[] a)
public static int hashCode(float[] a)
public static int hashCode(double[] a)
public static int hashCode(Object[] a)
基于指定数组的内容返回哈希码。对于任何两个满足Arrays.equals(a, b)的数组,其哈希码相同。

举例:
String[] array1 = {"Red", "Green", "Blue"};
String[] array2 = {"Red", "Green", "Blue"};
String[] array3 = {"Red", "Blue", "Green"};
Arrays.hashCode(array1); // 返回-2072969593
Arrays.hashCode(array2); // 返回-2072969593
Arrays.hashCode(array3); // 返回212215353

9.对数组所有元素进行升序排序
public static void sort(boolean[] a)
public static void sort(byte[] a)
public static void sort(char[] a)
public static void sort(short[] a)
public static void sort(int[] a)
public static void sort(long[] a)
public static void sort(float[] a)
public static void sort(double[] a)
public static void sort(Object[] a)
对指定的数组按升序进行排序。

举例:
String[] array = {"Red", "Green", "Blue"};
Arrays.sort(array); // 数组结果为{"Blue", "Green", "Red"}

10.对数组指定元素进行升序排序
public static void sort(boolean[] a, int fromIndex, int toIndex)
public static void sort(byte[] a, int fromIndex, int toIndex)
public static void sort(char[] a, int fromIndex, int toIndex)
public static void sort(short[] a, int fromIndex, int toIndex)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(long[] a, int fromIndex, int toIndex)
public static void sort(float[] a, int fromIndex, int toIndex)
public static void sort(double[] a, int fromIndex, int toIndex)
public static void sort(Object[] a, int fromIndex, int toIndex)
对指定的数组按升序进行排序,排序的元素范围为从fromIndex到toIndex-1索引处的元素。如果to-1超出索引范围则抛出异常。

举例:
String[] array = {"Red", "Green", "Blue", "Black"};
Arrays.sort(array, 1, 1); // 数组结果为{"Red", "Green", "Blue", "Black"}
Arrays.sort(array, 0, 1); // 数组结果为{"Red", "Green", "Blue", "Black"}
Arrays.sort(array, 1, 3); // 数组结果为{"Red", "Blue", "Green", "Black"}
Arrays.sort(array, 1, 4); // 数组结果为{"Red", "Black", "Green", "Blue"}

11.返回数组内容的字符串
public static String toString(boolean[] a)
public static String toString(byte[] a)
public static String toString(char[] a)
public static String toString(short[] a)
public static String toString(int[] a)
public static String toString(long[] a)
public static String toString(float[] a)
public static String toString(double[] a)
public static String toString(Object[] a)
返回指定数组内容的字符串表示形式。

举例:
String[] array = {"Red", "Green", "Blue"};
Arrays.toString(array); // 返回[Red, Green, Blue]
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值