【问题标题】:Is there a way to sort a String in java alphabetically without putting the String into an Array?有没有办法在不将字符串放入数组的情况下按字母顺序对java中的字符串进行排序?
【发布时间】:2017-12-05 00:20:41
【问题描述】:

在下面的代码中,我尝试将 i 处的 char 与 i+1 处的 char 进行比较。我的理解是,通过使用 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);
    }
}

【问题讨论】:

  • 你避免数组的目的是什么?使用数组或List 等可变数据结构是执行排序等操作的预期方式。
  • 另外,您永远不会更新 word 的值,所以这段代码只是再次打印原始输入。

标签: java arrays string char alphabet


【解决方案1】:

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

哪些输出

atchw

或者只使用一个数组(同样的结果)

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

【讨论】:

    【解决方案2】:

    使用此代码按字母顺序对字符串数组进行排序,而不存储在任何数组中

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

    【讨论】:

      【解决方案3】:

      要按字母顺序对字符串进行排序,您需要将每个字符与所有字符进行比较,如果满足条件,则交换字符。 这使用多个循环显示。最后我打印 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]);
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 2014-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多