java 按字母顺序排列给定字符串中的单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15597223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:12:01  来源:igfitidea点击:

Arranging words within a given string alphabetically

java

提问by Ashin Banerjee

i'm just trying to write a code for arranging the words within a string alphabetically.. But whenever i am running this it' entering in an infinite loop.. i cant figure out what is exactly happening..can anybody help me out plzzz.. below i've attached my code.

我只是想写一个代码来按字母顺序排列字符串中的单词..但是每当我运行它时,它就会进入一个无限循环..我无法弄清楚到底发生了什么..有人能帮我解决吗? .. 下面我附上了我的代码。

public class AscendString {
    String s=new String();

    public AscendString(String x)
    {
        s=x.trim();
    }

    public int NoWords()
    {
        int i=0;
        String s1=new String();
        s1=s;
        while(s1.length() > 0)
        {   i++;
            int j=s1.indexOf(' ');
            if(j>0)
            {
                s1.substring(j+1);
                s1=s1.trim();
            }
            else
            s1="";
        }
        return i;
    }

    public void Ascend()
    {
        String str[]=new String[NoWords()];
        String s1=new String();
        s1=s;
        int i=0;
        while(s1.length() > 0)
        {
            int j=s1.indexOf(' ');
            if(j>0)
            {
                str[i]=s1.substring(0,j) ;
                s1=s1.substring(j+1);
                s1=s1.trim();
                i++;
            }
            else
            {
                str[i]=s1;
                s1="";
            }
        }
        for(int j=0;j < str.length-1;j++)
        {
            for(int k=0;k < str.length-1-j;k++)
            if(str[k].length() > str[k+1].length())
            {String temp=str[k];
                str[k]=str[k+1];
                str[k+1]=temp;
            }
        }
        String str1="";
        for(int n=0;n < str.length;n++)
        str1=str1+str[n] +" " ;
        System.out.println("The String in Alphabetic Order is: "+str1);
    }

    public static void main(String args[])
    {
        AscendString exmpl=new AscendString("I Love Java Programming");
        exmpl.Ascend();
    }
}

回答by Cratylus

Why are you making this hard?

你为什么要这么难?

String exmpl=new String("I Love Java Programming");  
String[] parts = exmpl.split("\s+");   
Arrays.sort(parts);  
StringBuilder sb = new StringBuilder();  
for(String s:parts){  
   sb.append(s);  
   sb.append(" ");  
}  

String sorted = sb.toString.trim();  

回答by BobTheBuilder

Try:

尝试:

s1 = s1.substring(j+1);

substringreturn the sub-string, not changing the string., so this loop:

substring返回子字符串,不改变字符串。,所以这个循环:

    while(s1.length() > 0)
    {   i++;
        int j=s1.indexOf(' ');
        if(j>0)
        {
            s1.substring(j+1);
            s1=s1.trim();
        }
        else
        s1="";
    }

Goes forever.

永远去。

回答by Eric Jablow

  1. There is never any reason to call new String(...).
  2. Should punctuation matter?
  3. What about repeated words?
  4. Do you want strings to sort caring about case? What do you expect from "A man, a plan, a canal, Panama."? "A Panama. a a canal man, plan,"
  1. 从来没有任何理由打电话new String(...)
  2. 标点符号重要吗?
  3. 重复的单词怎么办?
  4. 你想要字符串排序关心大小写吗?你对“一个人,一个计划,一条运河,巴拿马”有什么期望?“一个巴拿马。一个运河人,计划,”

Adapting Cratylus' answer, you might change the regex to \\W+. You'd be splitting on runs of non-word-constituents. And then, learn about Collatorand CollationKey. You probably want case-insensitive searches. Or, lower-case the string on the way in.

根据 Cratylus 的回答,您可能会将正则表达式更改为\\W+. 你会分裂非单词成分的运行。然后,了解CollatorCollationKey。您可能想要不区分大小写的搜索。或者,将输入的字符串小写。