在Java中按字典顺序对单词进行排序
单词按字典顺序或字典顺序排序。这意味着单词将根据其组成字母按字母顺序排序。一个例子如下。
The original order of the words is Tom Anne Sally John The lexicographical order of the words is Anne John Sally Tom
演示此过程的程序如下。
示例
public class Example {
public static void main(String[] args) {
String[] words = { "Peach", "Orange", "Mango", "Cherry", "Apple" };
int n = 5;
System.out.println("The original order of the words is: ");
for(int i = 0; i < n; i++) {
System.out.println(words[i]);
}
for(int i = 0; i < n-1; ++i) {
for (int j = i + 1; j < n; ++j) {
if (words[i].compareTo(words[j]) > 0) {
String temp = words[i];
words[i] = words[j];
words[j] = temp;
}
}
}
System.out.println("\nThe lexicographical order of the words is: ");
for(int i = 0; i < n; i++) {
System.out.println(words[i]);
}
}
}输出结果
The original order of the words is: Peach Orange Mango Cherry Apple The lexicographical order of the words is: Apple Cherry Mango Orange Peach
热门推荐
1 带狗狗训练
4 听力训练狗狗
6 怎训练狗狗
7 腹肌训练狗狗
8 网球训练狗狗
9 记叙文训练狗狗
10 狗狗太凶怎么训练
11 核心力量训练狗狗
12 狗狗乱叫怎么训练
13 发音训练狗狗
14 怎么训练狗狗叼东西给主人
15 荆州训练狗狗
16 篮球训练狗狗
17 做训练狗狗
18 训练狗狗坐姿