Java程序以不区分大小写的顺序对数组进行排序
可以使用java.util.Arrays.sort()方法以不区分大小写的顺序对数组进行排序。另外,java.text.Collator类也是必需的,因为Collator.getInstance()用于获取所需语言环境的Collator对象。
演示此的程序如下所示-
示例
import java.text.Collator;import java.util.Arrays;
public class Demo {
public static void main(String args[]) {
String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" };
System.out.print("The unsorted array is: ");
System.out.println(Arrays.toString(arr));
Arrays.sort(arr, Collator.getInstance());
System.out.print("The sorted array in case-insensitive order is: ");
System.out.println(Arrays.toString(arr));
}
}
输出结果
The unsorted array is: [apple, mango, Banana, Melon, orange]The sorted array in case-insensitive order is: [apple, Banana, mango, Melon, orange]
现在让我们了解上面的程序。
首先定义数组arr []。然后打印未排序的数组。演示这的代码片段如下-
String[] arr = new String[] { "apple", "mango", "Banana", "Melon", "orange" };System.out.print("The unsorted array is: ");
System.out.println(Arrays.toString(arr));
Arrays.sort(arr,Collator.getInstance())方法用于按不区分大小写的顺序对数组进行排序。然后显示排序后的数组。演示这的代码片段如下-
Arrays.sort(arr, Collator.getInstance())System.out.print("The sorted array in case-insensitive order is: ");
System.out.println(Arrays.toString(arr));
以上是 Java程序以不区分大小写的顺序对数组进行排序 的全部内容, 来源链接: utcz.com/z/343293.html