Java中如何使用数组函数实现数组排序和查找?
发布时间:2023-06-14 00:14:43
Java中提供了很多数组函数,可以方便地实现数组的排序和查找功能。下面分别介绍一下Java中常用的数组函数和它们的使用方法。
一、数组排序函数
1. Arrays.sort()函数
Arrays.sort()函数是Java中提供的最常用的数组排序函数,它可以对数组进行排序,包括升序排列和降序排列。
语法:
public static void sort(int[] a) //升序排列 public static void sort(int[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(Object[] a) //升序排列 public static <T> void sort(T[] a, Comparator<? super T> c) //自定义比较器升序排列 public static void sort(char[] a) //升序排列 public static void sort(char[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(byte[] a) //升序排列 public static void sort(byte[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(short[] a) //升序排列 public static void sort(short[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(float[] a) //升序排列 public static void sort(float[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(double[] a) //升序排列 public static void sort(double[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(long[] a) //升序排列 public static void sort(long[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void sort(Object[] a, int fromIndex, int toIndex) //升序排列指定范围内的数组元素 public static void parallelSort(int[] a) //并行升序排列 public static void parallelSort(int[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(long[] a) //并行升序排列 public static void parallelSort(long[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(short[] a) //并行升序排列 public static void parallelSort(short[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(byte[] a) //并行升序排列 public static void parallelSort(byte[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(char[] a) //并行升序排列 public static void parallelSort(char[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(float[] a) //并行升序排列 public static void parallelSort(float[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static void parallelSort(double[] a) //并行升序排列 public static void parallelSort(double[] a, int fromIndex, int toIndex) //并行升序排列指定范围内的数组元素 public static <T> void parallelSort(T[] a) //并行升序排列 public static <T> void parallelSort(T[] a, int fromIndex, int toIndex) //并行升序排序指定范围内的数组元素 public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) //自定义比较器并行升序排列 public static IntStream asList(int... array) //返回基本类型int的Stream视图 public static IntStream stream(int[] array) //返回基本类型int的Stream视图
使用方法:
public static void main(String[] args) {
int[] arr = {3, 5, 2, 8, 4, 1, 9, 7, 6};
Arrays.sort(arr); //默认升序排列
System.out.println(Arrays.toString(arr)); //[1, 2, 3, 4, 5, 6, 7, 8, 9]
int[] arr1 = {3, 5, 2, 8, 4, 1, 9, 7, 6};
Arrays.sort(arr1, 2, 6); //指定范围升序排列
System.out.println(Arrays.toString(arr1)); //[3, 5, 1, 2, 4, 8, 9, 7, 6]
int[] arr2 = {3, 5, 2, 8, 4, 1, 9, 7, 6};
Arrays.parallelSort(arr2); //并行升序排列
System.out.println(Arrays.toString(arr2)); //[1, 2, 3, 4, 5, 6, 7, 8, 9]
//自定义比较器升序排列
String[] strs = {"hello", "world", "java", "python"};
Arrays.sort(strs, (String s1, String s2) -> s1.length() - s2.length());
System.out.println(Arrays.toString(strs)); //[java, hello, world, python]
}
2. Arrays.parallelSort()函数
Arrays.parallelSort()函数和Arrays.sort()函数类似,也是对数组进行排序,不同的是它使用了多线程技术,所以排序速度更快。
语法和使用方法和Arrays.sort()函数一样,这里就不再赘述。
二、数组查找函数
1. Arrays.binarySearch()函数
Arrays.binarySearch()函数是用来在有序数组中查找指定元素的索引值的函数。它返回要查找的元素在数组中的索引值,如果数组中不存在该元素,则返回负数。如果数组中有多个与要查找的元素相等的元素,那么无法确定返回哪一个元素的索引值。
语法:
public static int binarySearch(int[] a, int key) //基本类型int的数组查找 public static int binarySearch(int[] a, int fromIndex, int toIndex, int key) //指定范围内的基本类型int的数组查找 public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) //泛型数组查找 public static <T> int binarySearch(T[] a, T key) //泛型数组查找
使用方法:
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int index1 = Arrays.binarySearch(arr, 5); //普通查找
System.out.println("index1 = " + index1); //4
int index2 = Arrays.binarySearch(arr, 2, 6, 5); //指定范围查找
System.out.println("index2 = " + index2); //-1
String[] strs = {"hello", "world", "java", "python"};
int index3 = Arrays.binarySearch(strs, "java"); //泛型数组查找
System.out.println("index3 = " + index3); //2
}
2. Arrays.copyOf()函数
Arrays.copyOf()函数可以用来复制一个数组,并返回一个新数组。如果复制后的数组长度大于原数组长度,则使用默认值填充新数组的剩余部分。
语法:
public static int[] copyOf(int[] original, int newLength) public static <T> T[] copyOf(T[] original, int newLength)
使用方法:
`java
public static void main(String[] args)
