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

有没有一种方法可以在java中按字母顺序对字符串进行排序,而不用将字符串放入数组中?,java,arrays,string,char,alphabet,Java,Arrays,String,Char,Alphabet,在下面的代码中,我尝试将I处的字符与I+1处的字符进行比较。我的理解是,通过使用charAt():我可以从字符串中提取字符并将其视为整数,并能够比较两个字符。代码的这一部分可以工作,但我认为代码中缺少了一些东西,因此无法打印所需的结果。除非这种对字符串中的字符进行排序的方法无效 public class stringAlphabetical { public static void main(String[] args){ String word="watch";

在下面的代码中,我尝试将I处的字符与I+1处的字符进行比较。我的理解是,通过使用charAt():我可以从字符串中提取字符并将其视为整数,并能够比较两个字符。代码的这一部分可以工作,但我认为代码中缺少了一些东西,因此无法打印所需的结果。除非这种对字符串中的字符进行排序的方法无效

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);
    }
}
公共类字符串按字母顺序排列{
公共静态void main(字符串[]args){
字符串word=“watch”;
布尔交换;
字符温度=“”;
做{
交换=假;
对于(inti=0;iword.charAt(i+1)){//if(a>b){
温度=a;
a=b;
b=温度;
}
}
}while(swapped==true);
System.out.println(word);
}
}

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);
或者只使用数组(用于相同的结果)


要按字母顺序对字符串排序,需要将每个字符与所有字符进行比较,如果条件满足,则交换字符。 这是使用多个循环显示的。最后我打印字符数组

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]);
        }

    }
publicstaticvoidmain(字符串[]args){
String watchString=“watch”;
int j;
焦炭温度;
char[]chars=watchString.toCharArray();
for(int i=0;i字符[i]){
温度=焦耳[i];
chars[i]=chars[j];
chars[j]=温度;
}
}
}

对于(int k=0;k使用此代码按字母顺序对字符串数组进行排序,而不存储在任何数组中

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

避免使用数组的目的是什么?使用数组或
列表等可变数据结构是执行排序等操作的理想方法。此外,您从不更新
word
的值,因此此代码只需再次打印原始输入。
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]);
        }

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