从文件中读取字符串并从 ArrayList 中按降序排序的 RadixSort
RadixSort that reads strings from a file and sorts in descending order from ArrayList
我正在开发一个利用 RadixSort 来阅读单词的程序
从文件中提取并使用教授给我的算法按升序排序(要求 RadixSort 按预期工作,所有条目必须是单词,并且所有单词的长度必须相同)。我写了我的 RadixSort class:
编辑:请忽略 initializeWords 方法中的注释。他们在那里供以后测试。
public class RadixSort implements RadixSortADT{
private ArrayList<String> lowercaseArray;
private ArrayList<LinkedQueue<String>> arrayQueues;
private String results;
public RadixSort(){
}
public RadixSort(ArrayList<String> w) {
lowercaseArray = new ArrayList<String>();
arrayQueues = new ArrayList<>();
initializeList();
initializeWords(w);
}
public void initializeList() {
for(int i = 0; i < 26; i++){
arrayQueues.add(new LinkedQueue<String>());
}
}
public void initializeWords(ArrayList<String> w) {
// Get size of first word and check length and if it is a character.
// Test all 26 letters of the alphabet.
for(int i = 0; i < w.size(); i++){
lowercaseArray.add(w.get(i).toLowerCase());
}
}
public void sort() {
int item = 0;
for(int i = lowercaseArray.get(0).length()-1; i>=0; i--){
for(int j = 0; j < lowercaseArray.size(); j++){
char character = lowercaseArray.get(j).charAt(i);
arrayQueues.get(character-97).enqueue(lowercaseArray.get(j));
}
item = 0;
for(int k = 0; k < arrayQueues.size(); k++){
while(!arrayQueues.isEmpty()){
lowercaseArray.get(item++).equals(arrayQueues.get(k).dequeue());
}
}
}
}
public String toString(){
for(String words: lowercaseArray){
results += " " + words + " ";
}
return results;
}
}
为了进一步参考,同样由我的教授提供的驱动程序如下所示:
public class RadixSortDriver {
public static void main(String[] args) throws FileNotFoundException{
int i = 0;
ArrayList<String> words = new ArrayList<>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the file to import words");
String filename = scan.nextLine();
//String filename = "four.txt";
Scanner inFile = new Scanner(new File(filename));
while(inFile.hasNext()) {
words.add(inFile.nextLine());
}
RadixSort r = new RadixSort(words);
System.out.println("Unsorted List:\n" + r);
r.sort();
System.out.println("\n\nSorted List:\n" + r);
}
}
但是我遇到了两个错误:
- 我的输出看起来像这样:
Unsorted List:
null home crew xray atom yarn goat barn kite love rain soap
显然 null 不是我的话;不过,其他人是正确的。我试过更改我的 toString 并使用不同的输入文件进行测试,但我不明白为什么会这样。
- 程序不会继续,因为我在这一行得到一个 EmptyCollectionException(正如它所说,队列是空的):
lowercaseArray.get(item++).equals(arrayQueues.get(k).dequeue());
但我想不通为什么;当我向我的教授展示我的代码时,她指出逻辑是正确的,所以我想我们都忽略了一些东西。
此外,我不知道如何让我的代码按降序打印。
非常感谢您的帮助。
使用 set()
更新您的 lowercaseArray
。 equals
用于测试是否相等。您用于重新填充 lowercaseArray
的 while
循环中也存在错误。它应该检查 arrayQueues.get(k).isEmpty()
.
固定码:
item = 0;
for (int k = 0; k < arrayQueues.size(); k++) {
while (!arrayQueues.get(k).isEmpty()) {
lowercaseArray.set(item++, arrayQueues.get(k).dequeue());
}
}
您的 toString
方法存在的问题是它附加到最初为空的字段 results
。您应该将其设为局部变量并将其初始化为空字符串:
public String toString() {
String results = "";
for (String words : lowercaseArray) {
results += " " + words + " ";
}
return results;
}
我正在开发一个利用 RadixSort 来阅读单词的程序 从文件中提取并使用教授给我的算法按升序排序(要求 RadixSort 按预期工作,所有条目必须是单词,并且所有单词的长度必须相同)。我写了我的 RadixSort class:
编辑:请忽略 initializeWords 方法中的注释。他们在那里供以后测试。
public class RadixSort implements RadixSortADT{
private ArrayList<String> lowercaseArray;
private ArrayList<LinkedQueue<String>> arrayQueues;
private String results;
public RadixSort(){
}
public RadixSort(ArrayList<String> w) {
lowercaseArray = new ArrayList<String>();
arrayQueues = new ArrayList<>();
initializeList();
initializeWords(w);
}
public void initializeList() {
for(int i = 0; i < 26; i++){
arrayQueues.add(new LinkedQueue<String>());
}
}
public void initializeWords(ArrayList<String> w) {
// Get size of first word and check length and if it is a character.
// Test all 26 letters of the alphabet.
for(int i = 0; i < w.size(); i++){
lowercaseArray.add(w.get(i).toLowerCase());
}
}
public void sort() {
int item = 0;
for(int i = lowercaseArray.get(0).length()-1; i>=0; i--){
for(int j = 0; j < lowercaseArray.size(); j++){
char character = lowercaseArray.get(j).charAt(i);
arrayQueues.get(character-97).enqueue(lowercaseArray.get(j));
}
item = 0;
for(int k = 0; k < arrayQueues.size(); k++){
while(!arrayQueues.isEmpty()){
lowercaseArray.get(item++).equals(arrayQueues.get(k).dequeue());
}
}
}
}
public String toString(){
for(String words: lowercaseArray){
results += " " + words + " ";
}
return results;
}
}
为了进一步参考,同样由我的教授提供的驱动程序如下所示:
public class RadixSortDriver {
public static void main(String[] args) throws FileNotFoundException{
int i = 0;
ArrayList<String> words = new ArrayList<>();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the file to import words");
String filename = scan.nextLine();
//String filename = "four.txt";
Scanner inFile = new Scanner(new File(filename));
while(inFile.hasNext()) {
words.add(inFile.nextLine());
}
RadixSort r = new RadixSort(words);
System.out.println("Unsorted List:\n" + r);
r.sort();
System.out.println("\n\nSorted List:\n" + r);
}
}
但是我遇到了两个错误:
- 我的输出看起来像这样:
Unsorted List: null home crew xray atom yarn goat barn kite love rain soap
显然 null 不是我的话;不过,其他人是正确的。我试过更改我的 toString 并使用不同的输入文件进行测试,但我不明白为什么会这样。
- 程序不会继续,因为我在这一行得到一个 EmptyCollectionException(正如它所说,队列是空的):
lowercaseArray.get(item++).equals(arrayQueues.get(k).dequeue());
但我想不通为什么;当我向我的教授展示我的代码时,她指出逻辑是正确的,所以我想我们都忽略了一些东西。
此外,我不知道如何让我的代码按降序打印。
非常感谢您的帮助。
使用 set()
更新您的 lowercaseArray
。 equals
用于测试是否相等。您用于重新填充 lowercaseArray
的 while
循环中也存在错误。它应该检查 arrayQueues.get(k).isEmpty()
.
固定码:
item = 0;
for (int k = 0; k < arrayQueues.size(); k++) {
while (!arrayQueues.get(k).isEmpty()) {
lowercaseArray.set(item++, arrayQueues.get(k).dequeue());
}
}
您的 toString
方法存在的问题是它附加到最初为空的字段 results
。您应该将其设为局部变量并将其初始化为空字符串:
public String toString() {
String results = "";
for (String words : lowercaseArray) {
results += " " + words + " ";
}
return results;
}