简体   繁体   English

有没有一种方法可以按字母顺序对Java中的字符串进行排序而不将字符串放入数组中?

[英]Is there a way to sort a String in java alphabetically without putting the String into an Array?

In the code below, I tried to compare char at i with the char at i+1. 在下面的代码中,我试图将i处的字符与i + 1处的字符进行比较。 My understanding is that by using charAt(): I can take the character from the string and treat it as integer and be able to compare two characters. 我的理解是,通过使用charAt():我可以从字符串中获取字符并将其视为整数,并且能够比较两个字符。 This part of the code works, but I think I am missing something in the code, hence it is not printing the desired result. 这部分代码有效,但是我认为我在代码中缺少某些内容,因此无法打印出预期的结果。 Unless this way of sorting characters in a String is not valid. 除非这种对字符串中的字符进行排序的方法无效。

public class stringAlphabetical {

    public static void main(String[] args){
        String word="watch";
        boolean swapped;
        char temp = ' ';
        do{
            swapped = false;
            for(int i=0;i<word.length()-1;i++){
                char a = word.charAt(i);
                char b = word.charAt(i+1);

                if(word.charAt(i)>word.charAt(i+1)){   // if (a>b) {
                   temp = a;
                   a = b;
                   b = temp;
                }
            }


        }while (swapped==true);

        System.out.println(word);
    }
}

Java String is immutable , so you will need to use a mutable class (like StringBuilder ) - (also, you are modifying char values, not a reference) and you don't need t . Java String不可变的 ,因此您将需要使用可变的类(例如StringBuilder )-(而且,您正在修改char值,而不是引用),并且不需要t

StringBuilder word = new StringBuilder("watch");
boolean swapped;
do {
    swapped = false;
    for (int i = 0; i < word.length() - 1; i++) {
        char a = word.charAt(i), b = word.charAt(i + 1);

        if (a > b) { // <-- this is fine.
            word.setCharAt(i, b);
            word.setCharAt(i + 1, a);
            swapped = true;
        }
    }
} while (swapped);
System.out.println(word);

Which outputs 哪个输出

atchw

Or just use an array (for the same result) 或者只使用一个数组(以获得相同的结果)

String word = "watch";
char[] c = word.toCharArray();
Arrays.sort(c);
System.out.println(new String(c));

Use this code for sort the String array via alphabetical order without storing in any array 使用此代码通过字母顺序对String数组排序,而不存储在任何数组中

    Scanner kbd = new Scanner(System.in);
    String input = kbd.nextLine();
    String sortedString = Stream.of(input.split("")).sorted().collect(Collectors.joining());
    System.out.print(sortedString);

To sort the string alphabetically, you need to compare each character with all the characters and if the condition satisfies then you swap the character. 要按字母顺序对字符串进行排序,需要将每个字符与所有字符进行比较,如果条件满足,则可以交换字符。 This is shown using multiple loops. 使用多个循环来显示。 Lastly I print the char array. 最后,我打印了char数组。

public static void main(String[] args){
        String watchString = "watch";
        int j;
        char temp;

        char[] chars = watchString.toCharArray();

        for (int i = 0; i <chars.length; i++) {

            for ( j = 0; j < chars.length; j++) {

                if(chars[j]>chars[i]){
                    temp=chars[i];
                    chars[i]=chars[j];
                    chars[j]=temp;
                }

            }

        }

        for(int k=0;k<chars.length;k++){
            System.out.print(chars[k]);
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM