java单词排序_在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 

System.out.println(words[i]);

}

for(int i = 0; i 

for (int j = i + 1; 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 

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值