我有一个按字母顺序对单词进行排序的程序,这些单词来自输入它们的用户(我使用的是 GUI)。不幸的是,由于我还不知道的原因,代码的“排序”部分有一些下划线,但我怀疑它与类/数组有关。有关如何纠正此问题的任何提示都会很棒!
在“公共”下,我创建了一个类和数组。
class Word{
String word;
Word(String _word) {
word= _word;
}
}
ArrayList <Word> small = new ArrayList <Word>(); //array for words...
在 GUI 上的“排序按钮”actionPerformed 下...
String word;
Word b = new Word(word); //these two lines stores words inputted by user
small.add(b); //second line
//begins to sort
for (int k = 0; k < word.length(); k++) {
word[k] = word; //underlined red "array required"
int x;
for (int i = 0; i < word.length(); i++) {
// Assume first letter is x
x = i;
for (int j = i + 1; j < word.length(); j++) {
if (word[j].compareToIgnoreCase(word[x]) < 0) {
//underlined red "array required"
x = j;
}
}
if (x != i) {
//swap the words if not in correct order
final String temp = word[i]; //underlined red "array required"
word[i] = word[x]; //underlined red "array required"
word[x] = temp; //underlined red "array required"
}
istArea.append(word[i] + "\n");// Output ascending order
//underlined red "array required"
}
}