Sort arrays with Arrays.sort()

Arrays.sort

  edit
This entry lacks an overview . Add relevant content to make the entry more complete, and it can be upgraded quickly. Come and edit it!
 
Chinese name
Arrays.sort
foreign name
No
Classification
java
Definition
sort array
 

Introductionedit

Array sorting in java.
Arrays.sort(*Array) needs to be packaged import java.util.*; or import java.util.Arrays;
Arrays.sort (array name) is an operation for sorting arrays, but this method is in the java.util package, so you need to import it first when you use it

exampleedit

// The following program is the sorting operation of the array, where the sort method is used to sort the array
import java.util. *;
public class array004
{
public static void main(String[] args)
{
int a[] = {4,32,45,32,65,32,2} ;
System.out.print("Array order before sorting:");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
Arrays.sort(a);// Array sorting method
System.out.print("\nArray sorted order:");
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.print("\n");
}
}
输出结果:
数组排序前的顺序:4 32 45 32 65 32 2
数组排序后的顺序:2 4 32 32 32 45 65
String [] str = {"a","e","f","g","h","i","b","c","d"};
  System.out.println(".toString="+Arrays.toString(str)); [1]   //打印出数组中所有数据
  System.out.println(".asList="+Arrays.asList(str));
  Arrays.sort(str);//对数组进行排序
  System.out.println(".toString="+Arrays.toString(str));//打印排序后数组中所有数据
  Arrays.sort(str,Collections.reverseOrder());//对数组进行 倒序
  System.out.println(".asList="+Arrays.asList(str));
  int flag = Arrays.binarySearch(str, "a"); [1]   //查找数组中 元素 的位置(数组下标从 0 开始)
  System.out.println("b的所在位置:"+flag);
  String [] str2 = new String[4];
  Arrays.fill(str2, "w");//为数组中每个数据同初值
  System.out.println("str2[]="+Arrays.toString(str2));
  String [][] s1 = {{"a","b","c","d"},{"a","b","e","f"}};
  System.out.println("s1[][]="+Arrays.deepToString(s1)); [1]   //打印出二维数组中的全部数据
更多Arrays类的详细使用可以参考引用安安DIY创作室里面的文章内容。

API文档编辑

sort
public static void sort(int[] a)对指定的 int 型数组按数字升序进行排序。 参数:a - 要排序的数组
sort
public static void sort(int[] a, int fromIndex, int toIndex)对指定 int 型数组的指定范围按数字升序进行排序。排序的范围从索引 fromIndex(包括)一直到索引 toIndex(不包括)。(如果 fromIndex==toIndex,则排序范围为空。)
该排序算法是一个经过调优的快速排序法,改编自 Jon L. Bentley 和 M. Douglas McIlroy 合著的 Engineering a Sort Function", Software-Practice and Experience Vol. 23(11) P. 1249-1265 (November 1993)。此算法在许多数据集上提供 n*log(n) 性能,这导致其他快速排序会降低二次型性能。
参数:a - 要排序的数组 fromIndex - 要排序的第一个元素的索引(包括) toIndex - 要排序的最后一个元素的索引(不包括) 抛出:IllegalArgumentException- 如果 fromIndex > toIndex ArrayIndexOutOfBoundsException- 如果 fromIndex < 0 或 toIndex > a.length

用法编辑

1.sort(byte[] a)
对指定的 byte 型数组按数字升序进行排序。
  sort(byte[] a, int fromIndex, int toIndex)
  对指定 byte 型数组的指定范围按数字升序进行排序。
  sort(char[] a)
  对指定的 char 型数组按数字升序进行排序。
  sort(char[] a, int fromIndex, int toIndex)
  对指定 char 型数组的指定范围按数字升序进行排序。
  sort(double[] a)
  对指定的 double 型数组按数字升序进行排序。
  sort(double[] a, int fromIndex, int toIndex)
  对指定 double 型数组的指定范围按数字升序进行排序。
  sort(float[] a)
  对指定的 float 型数组按数字升序进行排序。
  sort(float[] a, int fromIndex, int toIndex)
  对指定 float 型数组的指定范围按数字升序进行排序。
  sort(int[] a)
  对指定的 int 型数组按数字升序进行排序。
  sort(int[] a, int fromIndex, int toIndex) [2]  
2.sort(long[] a)

  对指定的 long 型数组按数字升序进行排序。
  sort(long[] a, int fromIndex, int toIndex)
  对指定 long 型数组的指定范围按数字升序进行排序。
  sort(Object[] a)
  根据元素的自然顺序,对指定对象数组按升序进行排序。
  sort(Object[] a, int fromIndex, int toIndex)
  根据元素的自然顺序,对指定对象数组的指定范围按升序进行排序。
  sort(short[] a)
  对指定的 short 型数组按数字升序进行排序。
  sort(short[] a, int fromIndex, int toIndex)
  对指定 short 型数组的指定范围按数字升序进行排序。
  sort(T[] a, Comparator<? super T> c)
  根据指定比较器产生的顺序对指定对象数组进行排序。
  sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c)
  根据指定比较器产生的顺序对指定对象数组的指定范围进行排序。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326840341&siteId=291194637