题目要求
java实现字符串中的字母排序并输出排序后的结果
分析
1、创建一个字符串,赋值并将字符逐个存进数组中。
String str = "chenughonghuiaikuangwantong1314";
char[] chars = str.toCharArray();2、对其进行排序
sort方法是Arrays类中的静态方法,可以直接利用类名进行调用。
static void sort(type [] a)
-
对指定的 type型数组按数字升序进行排序。
立即学习“Java免费学习笔记(深入)”;
默认为升序排列
static void sort(type [] a, int fromIndex, int toIndex)
对指定数组的指定范围按数字升序进行排序。
type可以指定为int,float,double,long,byte等a- 要排序的数组
MD5校验和计算小程序(C)下载C编写,实现字符串摘要、文件摘要两个功能。里面主要包含3个文件: Md5.cpp、Md5.h、Main.cpp。其中Md5.cpp是算法的代码,里的代码大多是从 rfc-1321 里copy过来的;Main.cpp是主程序。
fromIndex- 要排序的第一个元素的索引(包括)toIndex- 要排序的最后一个元素的索引(不包括)
3、通过for循环将循环打印出来
正序打印
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}倒序打印
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]);
}java 代码
import java.util.Arrays;
public class characterSorting {
public static void main(String[] args) {
String str = "chenughonghuiaikuangwantong1314";
System.out.println("原字符串:"+str);
char[] chars = str.toCharArray();
Arrays.sort(chars);
//正序遍历输出
System.out.println("正序输出:");
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
//倒序遍历输出
System.out.println();
System.out.println("倒序输出:");
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]);
}
}
}运行结果

切记先写psvm!!!!!!(我在这翻沟了0.0)










