当前位置:网站首页>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);
}
}
边栏推荐
- 备份 TiDB 集群到持久卷
- Airiot helps the urban pipe gallery project, and smart IOT guards the lifeline of the city
- 【mysql篇-基础篇】事务
- Machine learning notes - explore object detection datasets using streamlit
- [award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
- 最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
- 如何挑选基金产品?2022年7月份适合买什么基金?
- OneSpin | 解决IC设计中的硬件木马和安全信任问题
- Mongodb learn from simple to deep
- Data island is the first danger encountered by enterprises in their digital transformation
猜你喜欢
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
How to test CIS chip?
使用高斯Redis实现二级索引
一键部署Redis任意版本
最新版本的CodeSonar改进了功能安全性,支持MISRA,C ++解析和可视化
I wrote a markdown command line gadget, hoping to improve the efficiency of sending documents by garden friends!
Implement secondary index with Gaussian redis
如何满足医疗设备对安全性和保密性的双重需求?
不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统
php 获取图片信息的方法
随机推荐
第二十章 使用工作队列管理器(三)
Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation
Force buckle 989 Integer addition in array form
网络原理(1)——基础原理概述
论文解读(ValidUtil)《Rethinking the Setting of Semi-supervised Learning on Graphs》
[concept of network principle]
Helix QAC 2020.2新版静态测试工具,最大限度扩展了标准合规性的覆盖范围
School 1 of vulnhub
如何挑选基金产品?2022年7月份适合买什么基金?
Nebula importer data import practice
c语言如何判定是32位系统还是64位系统
CJSON内存泄漏的注意事项
使用 BR 恢复 Azure Blob Storage 上的备份数据
Force buckle 2319 Judge whether the matrix is an X matrix
Chapter 20 using work queue manager (3)
php 获取图片信息的方法
写了个 Markdown 命令行小工具,希望能提高园友们发文的效率!
TS quick start - Generic
One click deployment of any version of redis
Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed