[英]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 aa
类a 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: 有两个原因:
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. 是的...方法名称很重要。
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: 问题在于字母顺序:
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.