当前位置:网站首页>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);
}
}
边栏推荐
- Oracle 存儲過程之遍曆
- Deep learning model compression and acceleration technology (VII): mixed mode
- Force buckle 912 Sort array
- Implement secondary index with Gaussian redis
- rk3128投影仪lcd显示四周显示不完整解决
- Force buckle 599 Minimum index sum of two lists
- Dachang classic pointer written test questions
- Is it safe to open a stock account at present? Can I open an account online directly.
- ASP. Net learning & ASP's one word
- Prometheus remote_write InfluxDB,unable to parse authentication credentials,authorization failed
猜你喜欢

Force buckle 2319 Judge whether the matrix is an X matrix

Measure the height of the building

Nebula importer data import practice

Cantata9.0 | 全 新 功 能

机器学习笔记 - 使用Streamlit探索对象检测数据集

MRS离线数据分析:通过Flink作业处理OBS数据

上海交大最新《标签高效深度分割》研究进展综述,全面阐述无监督、粗监督、不完全监督和噪声监督的深度分割方法

【论文阅读】MAPS: Multi-agent Reinforcement Learning-based Portfolio Management System

一. 基础概念

机械臂速成小指南(十二):逆运动学分析
随机推荐
Force buckle 599 Minimum index sum of two lists
Precautions for cjson memory leakage
Useful win11 tips
Splicing and splitting of integer ints
rk3128投影仪lcd显示四周显示不完整解决
Force buckle 2315 Statistical asterisk
目前股票开户安全吗?可以直接网上开户吗。
[award publicity] issue 22 publicity of the award list in June 2022: Community star selection | Newcomer Award | blog synchronization | recommendation Award
ASP. Net learning & ASP's one word
JNI 初级接触
Force buckle 1037 Effective boomerang
Mrs offline data analysis: process OBS data through Flink job
第二十章 使用工作队列管理器(三)
AADL Inspector 故障树安全分析模块
[solution] package 'XXXX' is not in goroot
MRS离线数据分析:通过Flink作业处理OBS数据
嵌入式系统真正安全了吗?[ OneSpin如何为开发团队全面解决IC完整性问题 ]
【哲思与实战】程序设计之道
图扑数字孪生煤矿开采系统,打造采煤“硬实力”
Force buckle 912 Sort array