当前位置:网站首页>Data sorting in string
Data sorting in string
2022-07-07 20:24:00 【Bear Bibi】
demand : There's a string :“91 27 46 38 50”, Please write a program to achieve the final output :“27 38 46 50 91”
analysis :
The first step is to separate the numbers in the string ( According to the space ) Put it in an array ;
Step 2 sort the array ;
The third step is to sort and convert it into string splicing again ;
Split the string according to the space :https://blog.csdn.net/yezonghui/article/details/106455940
Concrete realization :
- Define a string
- Store the numeric data in the string into a int In an array of types ;
public String[] split(String regex) : Get every digital data
public static int parseInt(String) : Store every data in int Array - Yes int Array to sort ;
- The sorted array elements are spliced to get a string
use StringBuilder To realize ; - Output results ;
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);
}
}
边栏推荐
- Force buckle 599 Minimum index sum of two lists
- 使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
- 浅尝不辄止系列之试试腾讯云的TUIRoom(晚上有约,未完待续...)
- Force buckle 1037 Effective boomerang
- Vulnhub's funfox2
- Flask1.1.4 werkzeug1.0.1 source code analysis: Routing
- ASP. Net learning & ASP's one word
- ISO 26262 - 基于需求测试以外的考虑因素
- Solve the problem that the executable file of /bin/sh container is not found
- MSE API learning
猜你喜欢
机械臂速成小指南(十二):逆运动学分析
ERROR: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your
使用高斯Redis实现二级索引
一. 基础概念
CIS芯片测试到底怎么测?
[philosophy and practice] the way of program design
一键部署Redis任意版本
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
随机推荐
ASP. Net learning & ASP's one word
想杀死某个端口进程,但在服务列表中却找不到,可以之间通过命令行找到这个进程并杀死该进程,减少重启电脑和找到问题根源。
Force buckle 599 Minimum index sum of two lists
使用 BR 备份 TiDB 集群数据到 Azure Blob Storage
When easygbs cascades, how to solve the streaming failure and screen jam caused by the restart of the superior platform?
字符串中数据排序
OneSpin 360 DV新版发布,刷新FPGA形式化验证功能体验
POJ 1742 coins (monotone queue solution) [suggestions collection]
AADL Inspector 故障树安全分析模块
Measure the height of the building
AIRIOT助力城市管廊工程,智慧物联守护城市生命线
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
Mrs offline data analysis: process OBS data through Flink job
CodeSonar网络研讨会
Micro service remote debug, nocalhost + rainbow micro service development second bullet
使用高斯Redis实现二级索引
整型int的拼接和拆分
PHP method of obtaining image information
POJ 1742 Coins ( 单调队列解法 )「建议收藏」
让这个CRMEB单商户微信商城系统火起来,太好用了!