当前位置:网站首页>字符串中数据排序
字符串中数据排序
2022-07-07 18:14:00 【恶熊比比】
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果:“27 38 46 50 91”
分析:
第一步把字符串中的数字分割出来(根据空格)放入一个数组中;
第二步将数组排序;
第三步排序完成再次转化成字符串拼接起来;
根据空格分割字符串:https://blog.csdn.net/yezonghui/article/details/106455940
具体实现:
- 定义一个字符串
- 把字符串中的数字数据存储到一个int类型的数组中;
public String[] split(String regex) :得到每一个数字数据
public static int parseInt(String) :把每一个数据存储到int数组中 - 对int数组进行排序;
- 排序后的数组元素进行拼接得到字符串
采用StringBuilder来具体实现; - 输出结果;
package Integerr;
import java.util.Arrays;
public class StringSort {
public static void main(String[] args) {
String s = "91 27 46 38 50"; // 1
String[] strArray = s.split(" "); // 2
int[] intArr = new int[strArray.length];
for (int i = 0; i < intArr.length; i++) {
intArr[i] = Integer.parseInt(strArray[i]);
}
Arrays.sort(intArr); //3
StringBuilder sb = new StringBuilder(); //4
for (int i = 0; i < intArr.length; i++) {
if (i == intArr.length - 1) {
sb.append(intArr[i]);
} else {
sb.append(intArr[i] + " ");
}
}
String s2 = new String(sb); //5
System.out.println(s2);
}
}
边栏推荐
- Try the tuiroom of Tencent cloud (there is an appointment in the evening, which will be continued...)
- kubernetes之创建mysql8
- 浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
- EasyGBS级联时,上级平台重启导致推流失败、画面卡住该如何解决?
- Oracle 存儲過程之遍曆
- 大厂经典指针笔试题
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- vulnhub之school 1
- CIS芯片测试到底怎么测?
- Some arrangements about oneself
猜你喜欢
Opencv学习笔记 高动态范围 (HDR) 成像
School 1 of vulnhub
One click deployment of any version of redis
使用高斯Redis实现二级索引
整型int的拼接和拆分
Machine learning notes - explore object detection datasets using streamlit
[philosophy and practice] the way of program design
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
How to test CIS chip?
随机推荐
第二十章 使用工作队列管理器(三)
equals 方法
Force buckle 459 Duplicate substring
Force buckle 88 Merge two ordered arrays
Some arrangements about oneself
Traversal of Oracle stored procedures
MIT科技评论文章:围绕Gato等模型的AGI炒作可能使人们忽视真正重要的问题
Splicing and splitting of integer ints
Micro service remote debug, nocalhost + rainbow micro service development second bullet
Machine learning notes - explore object detection datasets using streamlit
Chapter 9 Yunji datacanvas company won the highest honor of the "fifth digital finance innovation competition"!
Sword finger offer II 013 Sum of two-dimensional submatrix
JVM 类加载机制
Open source heavy ware! Chapter 9 the open source project of ylarn causal learning of Yunji datacanvas company will be released soon!
How to cooperate among multiple threads
sql 常用优化
JNI 初级接触
九章云极DataCanvas公司摘获「第五届数字金融创新大赛」最高荣誉!
Vulnhub tre1
MRS离线数据分析:通过Flink作业处理OBS数据