对输入字符串内的字符进行排序后(按ASCII码进行升序排序)输出。java,不用Arrays
时间: 2024-04-29 14:21:06 浏览: 12
可以使用冒泡排序来实现:
```
public static void main(String[] args) {
String str = "hello world";
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length - 1; i++) {
for (int j = 0; j < chars.length - i - 1; j++) {
if (chars[j] > chars[j + 1]) {
char temp = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = temp;
}
}
}
for (int i = 0; i < chars.length; i++) {
System.out.print(chars[i]);
}
}
```
相关问题
对输入字符串内的字符进行排序后(按ASCII码进行升序排序)输出。java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
System.out.println(new String(charArray));
}
}
java对一个字符串进行ASCII排序
可以使用Java内置的排序函数Arrays.sort()和字符串的toCharArray()方法来实现ASCII排序,具体代码如下:
```java
import java.util.Arrays;
public class ASCIISorter {
public static void main(String[] args) {
String str = "hello";
char[] chars = str.toCharArray();
Arrays.sort(chars);
String sortedStr = new String(chars);
System.out.println("Sorted string: " + sortedStr);
}
}
```
运行结果如下:
```
Sorted string: ehllo
```
这里将字符串转换为字符数组,然后使用Arrays.sort()函数对字符数组进行排序,最后再将排序后的字符数组转换回字符串即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)