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]
 

相关内容

热门资讯

穆某(17岁)、李某(18岁)... 11月20日,最高人民法院发布涉未成年人网络保护典型案例,其中包括:被告人李某(18周岁)、穆某(1...
G20宣言破例首日表决,压倒性... 【文/观察者网 阮佳琪】 当地时间11月22日至23日,二十国集团(G20)领导人第二十次峰会在南...
但斌发微博晒东方港湾海外基金排... 红星资本局11月22日消息,今日,深圳东方港湾投资管理股份有限公司(以下简称“东方港湾”)董事长但斌...
宁夏回族自治区网信办主任宋世文... 近日,宁夏回族自治区体育局官网“机构职能信息”栏目更新显示,宋世文已任自治区体育局党组书记、局长。 ...
U16国足首战巴林:决战亚预赛... 11月22日,2026亚足联U17亚洲杯预选赛A组的比赛正式拉开帷幕,U16国足将在主场迎战本届预选...
律师起诉徐州东站“便捷换乘”不... 近日,律师殷清利向九派新闻反映,其因高铁中转换乘不便,将徐州东站诉至法庭。7月,徐州铁路运输法院出具...
高市早苗启程飞赴G20,日媒疯... 哪怕我国外交部已经再三强调中方没有在二十国集团峰会期间会见日本领导人的安排,日本方面仍未放弃搞“碰瓷...
直接爆单!浙江老板忙坏了:一天... 每年11月的第四个星期五,是一些欧洲国家的年末消费季,也被称为“黑色星期五”。 今年的“黑五”虽然还...
高市早苗被曝G20峰会迟到近一... ▲新京报我们视频出品(ID:wevideo) 据环球网报道,11月22日,日本首相高市早苗被曝在G2...
因侵害商标权纠纷,慕思股份起诉... 天眼查APP显示,近日,慕思健康睡眠股份有限公司新增一则开庭公告,案由为“侵害商标权纠纷”,原告为慕...