当前位置:网站首页>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);
}
}
边栏推荐
- CUDA versions are inconsistent, and errors are reported when compiling apex
- VMWare中虚拟机网络配置
- Chapter 20 using work queue manager (3)
- 微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
- Deep learning model compression and acceleration technology (VII): mixed mode
- 实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
- 如何满足医疗设备对安全性和保密性的双重需求?
- 让这个CRMEB单商户微信商城系统火起来,太好用了!
- Force buckle 674 Longest continuous increasing sequence
- TS quick start - Generic
猜你喜欢

Don't fall behind! Simple and easy-to-use low code development to quickly build an intelligent management information system

H3C S7000/S7500E/10500系列堆叠后BFD检测配置方法

智能软件分析平台Embold

Force buckle 2319 Judge whether the matrix is an X matrix

如何满足医疗设备对安全性和保密性的双重需求?

不落人后!简单好用的低代码开发,快速搭建智慧管理信息系统

ASP.NET学习& asp‘s one word

测量楼的高度

机械臂速成小指南(十二):逆运动学分析

让这个CRMEB单商户微信商城系统火起来,太好用了!
随机推荐
How to implement safety practice in software development stage
The boundary of Bi: what is bi not suitable for? Master data, Martech? How to expand?
Force buckle 459 Duplicate substring
Force buckle 1961 Check whether the string is an array prefix
《数字图像处理原理与实践(MATLAB版)》一书之代码Part2[通俗易懂]
有了ST7008, 蓝牙测试完全拿捏住了
Mongodb learn from simple to deep
【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System
Network principle (1) - overview of basic principles
Kubernetes -- detailed usage of kubectl command line tool
华为CE交换机下载文件FTP步骤
Force buckle 674 Longest continuous increasing sequence
How to test CIS chip?
Vulnhub tre1
MSE API learning
【解决】package ‘xxxx‘ is not in GOROOT
实战:sqlserver 2008 扩展事件-XML转换为标准的table格式[通俗易懂]
Lingyun going to sea | saihe & Huawei cloud: jointly help the sustainable development of cross-border e-commerce industry
微服务远程Debug,Nocalhost + Rainbond微服务开发第二弹
Apifox interface integrated management new artifact