目录

static void sort(char[] a, int fromIndex, int toIndex)

描述 (Description)

java.util.Arrays.sort(char[] a, int fromIndex, int toIndex)方法将指定的字符数组的指定范围按数字升序排序。 要排序的范围从索引fromIndex (包括)延伸到index toIndex ,exclusive。

声明 (Declaration)

以下是java.util.Arrays.sort()方法的声明

public static void sort(char[] a, int fromIndex, int toIndex)

参数 (Parameters)

  • a - 这是要排序的数组。

  • fromIndex - 这是要排序的第一个元素(包括)的索引。

  • toIndex - 这是要排序的最后一个元素(不包括)的索引。

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

  • IllegalArgumentException - 如果fromIndex“toIndex

  • ArrayIndexOutOfBoundsException - 如果fromIndex“0或toIndex”a.length

例子 (Example)

以下示例显示了java.util.Arrays.sort()方法的用法。

package com.iowiki;
import java.util.Arrays;
public class ArrayDemo {
   public static void main(String[] args) {
      // initializing unsorted char array
      char aArr[] = {'q','p','s','r'};
      // let us print all the values available in list
      for (char value : cArr) {
         System.out.println("Value = " + value);
      }
      // sorting array from index 0 to 3
      Arrays.sort(cArr, 0, 3);
      // let us print all the values available in list
      System.out.println("char array with some sorted values(0 to 3) is:");
      for (char number : cArr) {
         System.out.println("Values = " + values);
      }
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

Value = q
Value = p
Value = s
Value = r
char array with some sorted values(0 to 3) is:
Value = p
Value = q
Value = s
Value = r
↑回到顶部↑
WIKI教程 @2018