Arrangement of words in a sentence alphabetically

ぃ、小莉子 提交于 2019-12-26 14:00:50

问题


Suppose we're given a sentence. Then, how would we arrange the words alphabetically? For example,

Sentence: Big Bang Theory is better than Two and a Half Men.

Arranged Sentence: a and Bang better Big Half is Men than Theory Two.

I was thinking of applying a loop through the alphabets and comparing it with the sentence, but I keep getting confused. I can't think of anything. Help!


回答1:


Use a StringTokenizer to tokenize the sentence and fill the words in a list. A StringTokenizer object uses the characters " \t\n\r\f" (the space character, the tab character, the newline character, the carriage-return character, and the form-feed character) by default as delimiters to split the sentence into tokens. Then call Collections#sort(List, Comparator) with a case-insensitive comparator.

String sentence = "Big Bang Theory is better than Two and a Half Men";
StringTokenizer tokenizer = new StringTokenizer(sentence);
List<String> words = new ArrayList<String>();
while(tokenizer.hasMoreTokens()) {
    words.add(tokenizer.nextToken());
}

Collections.sort(words, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        return o1.compareToIgnoreCase(o2);
    }
});

In the above code, a custom comparator is added to use String.compareToIgnoreCase to ignore case when comparing two strings. If you don't provide a custom comparator, you would end up with a list of words sorted as Bang, Big, Half, Men, Theory, Two, a, and, better, is, than.

Read the Javadocs of the relevant classes:

http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

Javadoc for Collections.sort:

http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)




回答2:


You can split the sentence into array of words, then sort this array with a defined Comparator class:

String sentence = "Big Bang Theory is better than Two and a Half Men";
String [] arr = sentence.split(" ");
Arrays.sort(arr, (String o1, String o2) -> o1.compareToIgnoreCase(o2));
for (String arr1 : arr) {
    System.out.println(arr1);
}



回答3:


You dont have to manually loop over to sort the array (It's already being done by the Arrays sort method). Instead you could use Arrays.sort method to sort each word in a sentence by splitting the sentence by space and by providing String's inbuilt comparator CASE_INSENSITIVE_ORDER which will say how exactly you want to sort the data (Comparing two strings lexicographically, ignoring case) by comparating one String with the other. For instance, you could do something like:

String line = "Big Bang Theory is better than Two and a Half Men";
    String[] strArray = line.split(" ");//split by space so we get sentence word by word
    Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
    StringBuilder stringBuilder = new StringBuilder(line.length());//recreate your sentence back.
    for (int i = 0; i < strArray.length - 1; i++) {
        stringBuilder.append(strArray[i]);
        stringBuilder.append(" ");
    }
    stringBuilder.append(strArray[strArray.length - 1]);
    System.out.println(stringBuilder.toString());


来源:https://stackoverflow.com/questions/28138452/arrangement-of-words-in-a-sentence-alphabetically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!