简体   繁体   English

不使用Array.sort()或compareTo()按字母顺序排列字符串数组?

[英]Alphabetize a string array without using Array.sort() or compareTo()?

I have an exercise in class where you take names from the user and output them alphabetically. 我在课堂上做一个练习,您在该练习中从用户那里取名字并按字母顺序输出它们。 I'm not allowed to use Array.sort() or compareTo() . 我不允许使用Array.sort()compareTo() I seem to have gotten it to work for the most part... Except for when I enter something like a aaa aa , it outputs it in the following order: 我似乎大部分时间都可以使用它...除了当我输入a aaa aaa aaa aa东西时,它按以下顺序输出:

aaa
aa
a

I'd really like to be able to output it in this order instead: 我真的很想能够按此顺序输出它:

a
aa
aaa

This is what I have so far: 这是我到目前为止的内容:

public static void main(String args[]) throws IOException {

    BufferedReader key =
    new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Alphabetizing names\n");

    System.out.println("Enter names separated by spaces:");
    StringTokenizer names1 = new StringTokenizer(key.readLine());

    int tokens = names1.countTokens();
    String[] names2 = new String[tokens];
    String y;

    for (int a = 0; a < tokens; a++) {

        names2[a] = names1.nextToken();

    }

    System.out.println("\nSorted names:");

    for (int a = 0; a < tokens; a++) {

        for (int b = a + 1; b < tokens; b++) {

            if(alphabetize(names2[a], names2[b])) {

                y = names2[a];
                names2[a] = names2[b];
                names2[b] = y;

            }

        }

    }

    for (int c = 0; c < tokens; c++) {

        System.out.println(names2[c]);

    }

}

static boolean alphabetize(String a, String b) {

    for(int c = 0; ; c++) {

        if((c == a.length()-1) && (c == b.length()-1)) {

            return false;

        }

        if(c == a.length()-1) {

            return true;

        }

        if(c == b.length()-1) {

            return false;

        }

        if((a.toLowerCase().charAt(c) - b.toLowerCase().charAt(c)) > 0) {

            return true;

        }

    }

}

Please help!! 请帮忙!! Thank you! 谢谢!

Hint 1: Look at the output. 提示1:查看输出。 What does it look like is happening? 它看起来像什么?

Hint 2: Based on the obvious conclusion from Hint 1 ... look at the alphabetize method ... and figure out why that causes what you are seeing. 提示2:基于提示1得出的明显结论……看一下alphabetize方法……并弄清楚为什么会导致您看到的内容。


Meta-hint: I think your problem here is that you don't have a consistent mental model of what alphabetize is supposed to be doing; Meta提示:我认为您的问题在于,对于alphabetize应该做什么,您没有一个一致的思维模式; ie the intended meaning of the result. 即结果的预期含义。 There are two reasons for this: 有两个原因:

  1. The name of the method is opaque. 该方法的名称是不透明的。 The word "alphabetize" is not a verb whose meaning maps to the action you are attempting to perform. 单词“ alphabetize”不是动词,其含义与您要尝试执行的动作相对应。 The http://www.thefreedictionary.com/alphabetize link says: http://www.thefreedictionary.com/alphabetize链接显示:
 alphabetize (ˈælfəbəˌtaɪz) or alphabetise. vb (tr) 1. to arrange in conventional alphabetical order 2. to express by an alphabet 

Your method is doing neither of those things. 您的方法都不做任何事情。

Yes ... method names are important. 是的...方法名称重要。

  1. You don't have any comments to explain what the method should return. 您没有任何注释可以解释该方法应返回的内容。 It is considered best practice to write a javadoc comment for any non-trivial method that forms the "contract" between the method and the code that calls the method. 最好的做法是为任何非平凡的方法编写javadoc注释,这些注释会在方法与调用该方法的代码之间形成“契约”。 In this case, you need a comment that says something like "this method returns true if X, Y or Z, and false otherwise" . 在这种情况下,您需要一个注释,例如“如果X,Y或Z,此方法返回true ,否则返回false

The problems are in alphabetizer: 问题在于字母顺序:

  1. First if : if a and b have the same length, you don't compare their last characters. 首先,如果 :如果a和b的长度相同,则不比较它们的最后一个字符。
  2. Second and Third if : if a and b have not the same length, you dont consider the last char of the shortest 第二和第三,如果 :如果a和b的长度不同,则不考虑最短的最后一个字符
  3. Fourth if : you only consider the case where the char read from a is higher than the char read from b, you should also consider the case where b is higher than a. 第四种情况 :如果仅考虑从a读取的字符高于从b读取的字符的情况,则还应考虑b大于a的情况。

Tip: The loop should continue only if the char that you read are equals, 提示:仅当您读取的字符等于时,循环才应继续,


Here how to solve your problem: 这里是如何解决您的问题的:

static boolean alphabetize(String a, String b) {
    char ca;
    char cb;
    for(int c = 0; ; c++) {
        ca = a.toLowerCase().charAt(c);
        cb = b.toLowerCase().charAt(c);
        if((c == a.length()-1) && (c == b.length()-1)) {
            return (ca - cb) > 0;
        } else if(c == a.length()-1) {
            return false;
        } else if(c == b.length()-1) {
            return true;
        } else if (ca - cb != 0) { //if are not equals
            return (ca - cb) > 0;
        }      
    }
}

I've tryed with many strings, for example this: 我尝试了很多字符串,例如:

aaa ccc arc abr ald old mal ald aaaa bbbb aaaa car cro arc dsjd qhjk hdjgsdaj asidasiodu asi

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

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