让我们首先创建一个字符串数组:
String[] strArr = new String[] { "r", "p", "v","y", "s", "q" };
现在,使用Arrays.sort()方法按字母顺序对数组进行排序。在这里,我们将顺序设置为不区分大小写:
Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);
import java.util.Arrays; public class Demo { public static void main(String[] args) { String[] strArr = new String[] { "r", "p", "v","y", "s", "q" }; System.out.println("Sort array in alphabetical order..."); Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER); for (int a = 0; a < strArr.length; a++) { System.out.println(strArr[a]); } } }
输出结果
Sort array in alphabetical order... p q r s v y