在Java中如何使用数组函数进行排序、查找和填充操作?
Java中的数组有很多常用的函数用于排序、查找和填充操作。这些函数可以让我们更方便地处理数组,提高开发效率,减少出错率,本文将介绍常用的数组函数及其用途。
一、排序函数
Java中的数组排序函数有两种,一种是Arrays.sort函数,另一种是Collections.sort函数。这两个函数都可以对数组进行排序,但它们的适用场景不同。
Arrays.sort函数
Arrays.sort函数是一个静态函数,用于对数组进行排序。它的语法如下:
public static void sort(int[] a)
public static void sort(int[] a, int fromIndex, int toIndex)
public static void sort(Object[] a)
public static void sort(Object[] a, int fromIndex, int toIndex)
public static void sort(T[] a, Comparator<? super T> c)
public static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
其中,a是要排序的数组,fromIndex是排序的起始位置,toIndex是排序的结束位置,c是自定义的比较器。
示例:
int[] arr1 = {5,2,7,3,9,1};
Arrays.sort(arr1);
for(int i=0;i<arr1.length;i++){
System.out.print(arr1[i]+" ");
}
输出:
1 2 3 5 7 9
Collections.sort函数
Collections.sort函数是一个非静态函数,用于对数组列表进行排序。它的语法如下:
public static <T> void sort(List<T> list)
public static <T> void sort(List<T> list, Comparator<? super T> c)
其中,list是要排序的数组列表,c是自定义的比较器。
示例:
List<Integer> list1 = new ArrayList<Integer>();
list1.add(5);
list1.add(2);
list1.add(7);
list1.add(3);
list1.add(9);
list1.add(1);
Collections.sort(list1);
for(int i=0;i<list1.size();i++){
System.out.print(list1.get(i)+" ");
}
输出:
1 2 3 5 7 9
二、查找函数
Java中的数组查找函数也有两种,分别是Arrays.binarySearch函数和Collections.binarySearch函数。这两个函数都可以在一个有序数组中查找指定元素,但它们的适用场景也不同。
Arrays.binarySearch函数
Arrays.binarySearch函数是一个静态函数,用于在一个有序数组中查找指定元素。它的语法如下:
public static int binarySearch(int[] a, int key)
public static int binarySearch(int[] a, int fromIndex, int toIndex, int key)
public static <T> int binarySearch(T[] a, T key)
public static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T 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)
其中,a是有序数组,fromIndex是查找的起始位置,toIndex是查找的结束位置,key是要查找的元素,c是自定义的比较器。
示例:
int[] arr2 = {1,2,3,5,7,9};
System.out.println(Arrays.binarySearch(arr2,5));
输出:
3
Collections.binarySearch函数
Collections.binarySearch函数是一个非静态函数,用于在一个有序数组列表中查找指定元素。它的语法如下:
public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key)
public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c)
其中,list是有序数组列表,key是要查找的元素,c是自定义的比较器。
示例:
List<Integer> list2 = new ArrayList<Integer>();
list2.add(1);
list2.add(2);
list2.add(3);
list2.add(5);
list2.add(7);
list2.add(9);
System.out.println(Collections.binarySearch(list2,5));
输出:
3
三、填充函数
Java中的数组填充函数有两种,一种是Arrays.fill函数,另一种是Collections.fill函数。这两个函数都可用于将数组或数组列表的元素填充为指定值。
Arrays.fill函数
Arrays.fill函数是一个静态函数,用于将数组的元素填充为指定值。它的语法如下:
public static void fill(int[] a, int val)
public static void fill(int[] a, int fromIndex, int toIndex, int val)
public static void fill(boolean[] a, boolean val)
public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val)
public static void fill(byte[] a, byte val)
public static void fill(byte[] a, int fromIndex, int toIndex, byte val)
public static void fill(char[] a, char val)
public static void fill(char[] a, int fromIndex, int toIndex, char val)
public static void fill(short[] a, short val)
public static void fill(short[] a, int fromIndex, int toIndex, short val)
public static void fill(long[] a, long val)
public static void fill(long[] a, int fromIndex, int toIndex, long val)
public static void fill(float[] a, float val)
public static void fill(float[] a, int fromIndex, int toIndex, float val)
public static void fill(double[] a, double val)
public static void fill(double[] a, int fromIndex, int toIndex, double val)
public static <T> void fill(T[] a, T val)
public static <T> void fill(T[] a, int fromIndex, int toIndex, T val)
其中,a是要填充的数组,fromIndex是填充的起始位置,toIndex是填充的结束位置,val是要填充的值。
示例:
int[] arr3 = new int[6];
Arrays.fill(arr3,0);
for(int i=0;i<arr3.length;i++){
System.out.print(arr3[i]+" ");
}
输出:
0 0 0 0 0 0
Collections.fill函数
Collections.fill函数是一个非静态函数,用于将数组列表的元素填充为指定值。它的语法如下:
public static <T> void fill(List<? super T> list, T obj)
其中,list是要填充的数组列表,obj是要填充的对象。
示例:
List<Integer> list3 = new ArrayList<Integer>(6);
Collections.fill(list3,0);
for(int i=0;i<list3.size();i++){
System.out.print(list3.get(i)+" ");
}
输出:
0 0 0 0 0 0
总结:
以上是Java中常用的数组函数,我们可以根据它们的用途来选择合适的函数进行操作。在开发中,熟练使用这些数组函数可以提高开发效率、加快开发速度,同时减少出错率。
