java.util.Arrays类使用简介
创始人
2025-05-30 01:59:03
0

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

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

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

String[] array = {"Red", "Green", "Blue"};
List 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 int binarySearch(T[] a, T key, Comparator c)
public static int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator 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[] copyOf(T[] original, int newLength)
public static T[] copyOf(U[] original, int newLength, Class 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[] copyOfRange(T[] original, int from, int to)
public static T[] copyOfRange(U[] original, int from, int to, Class 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]
 

相关内容

热门资讯

加大政策协同力度,提升“一老一... 齐鲁晚报·齐鲁壹点记者 李文璇 6月16日下午,山东省政协召开月度协商会,围绕“推动建设‘一老一小’...
原创 多... 近期,全国多个省市相继宣布暂停汽车消费补贴政策,这一现象迅速成为社会关注焦点。根据各地官方公告,河南...
秦都区陈杨寨街道:政策宣传“零... 阳光讯(记者 刘金 通讯员 任珊 文/图)6月16日,一场别开生面的政策宣传活动在金泰社区广场拉开帷...
支持外贸高质量发展 成都经开区... △活动现场 6月16日,由成都经开区商务局主办的2025成都经开区外向型企业交流对接暨外贸高质量发展...
福安药业:将持续坚持实施积极分... 金融界6月16日消息,有投资者在互动平台向福安药业提问:贵公司股价近三四年一直萎靡不振是不是贵司不作...
上海离婚律师-婚姻律师:俞强律... 婚姻破裂时,一套共同还贷多年的房产背后,藏着无数个日夜的付出与期待。 2018年,甲男以个人名义签...
国常会部署上海自贸试验区经验复... 近日召开的国务院常务会议部署中国(上海)自由贸易试验区试点措施复制推广工作。会议提出,要用好中国(上...
雅本化学发布融资与对外担保管理... 2025年6月16日,雅本化学股份有限公司发布公告,公布了其融资与对外担保管理制度。 该制度旨在规范...
科技保险迎来政策红利期,如何撑... 科技保险作为支持科技创新的金融工具之一,重要性日益凸显。6月16日,上海出台科技保险新政,强化保险服...
中行北京分行携手北京海关举办A... 6月16日,北京海关联合中国银行北京市分行(以下简称“中行北京分行”)举办“北京海关-中国银行北京市...