简体   繁体   English

难以按字母顺序反转链表

[英]Difficulty reversing a linked list alphabetically

I'm having trouble reversing a LinkedList.我在反转 LinkedList 时遇到问题。 In other words, I need them sorted in za order (in contrast to az).换句话说,我需要它们按 za 顺序排序(与 az 相对)。 I've tried Collections.reverse but is not coming into effect?我试过 Collections.reverse 但没有生效? I have the following:我有以下几点:

import java.io.*;
import java.util.*;
public class pa9Driver {
//create two list
//1st List is of type word class
//2nd list is of type Anagram_Family
public static List<Word> words = new LinkedList<Word>();
public static List<AnagramFamily> familyList = new LinkedList<AnagramFamily>();

//a main method for driver class
public static void main(String[] args) {
//call the generate method to read word from the file
generate_WordList();
//sort the word list
Collections.sort(words);
//generate the anagram family for the word
generate_FamilyList();
//sort the anagram family list      
Collections.sort(familyList, new anagramFamilyComparator());
//reverse the anagram family list
Collections.reverse(familyList);
topFive();
}//main ends

public static void topFive() {
int i;
for(i = 0; i < 15; i++) {
System.out.print(familyList.get(i).getCanonicalForm1() + ", ");
System.out.print(familyList.get(i).getSize() + ": ");
System.out.println(familyList.get(i));
   }
}

//method that read word
public static void generate_WordList() {
File inFile12=new File("words.txt");
Scanner fileRead1=null;
try {
fileRead1 = new Scanner(inFile12);
} catch (Exception exe) {
       exe.printStackTrace();
       System.exit(0);
   }
  
   //until the file has words read the words
   while(fileRead1.hasNext()) {
       words.add(new Word(fileRead1.next()));
   }
}
//generate the anagram and add it to the current family
public static void generate_FamilyList() {
Iterator<Word> readWord1 = words.iterator();
Word previousWord1 = words.get(0);
familyList.add(new AnagramFamily());
int index1 = 0;
while(readWord1.hasNext()) {
Word currentWord1 = readWord1.next();
if(currentWord1.getCanonicalForm1().equals(previousWord1
.getCanonicalForm1())) {
familyList.get(index1).add(currentWord1);
} else {
index1++;
familyList.add(new AnagramFamily());
familyList.get(index1).add(currentWord1);
  }
previousWord1 = currentWord1;
    }
  }
}

For convenience sake, I'll only show the first few lines of code that I have and that is expected.为方便起见,我将只展示我拥有的和预期的前几行代码。 Currently:目前:

[apers, apres, asper, pares, parse, pears, prase, presa, rapes, reaps, spare, spear] [apers,apres,asper,pares,解析,梨,prase,presa,强奸,收割,备用,矛]

[alerts, alters, artels, estral, laster, ratels, salter, slater, staler, stelar, talers] [警报,alters,artels,estral,laster,ratels,salter,slater,staler,stelar,talers]

Expected:预期的:

[spear, spare, reaps, rapes, presa, prase, pears, parse, pares, asper, apres, apers] [矛,备用,收割,强奸,presa,prase,梨,解析,pares,asper,apres,apers]

[talers, stelar, staler, slater, salter, ratels, laster, estral, artels, alters, alerts] [talers,stelar,staler,slater,salter,ratels,laster,estral,artels,alters,alerts]

Try:尝试:

Collections.sort(familyList, Comparator.reverseOrder());

Alternatively, you can do:或者,您可以执行以下操作:

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

//Print list in reverse order
        for(String st : familyList){
            System.out.println(st);
        }

Seeing your code, it seems AnagramFamily is a kind of List , which you are not sorting.看到您的代码,似乎AnagramFamily是一种List ,您没有对其进行排序。

You need to sort the AnagramFamily (List of String) with a StringComparator for getting your desired output.您需要使用StringComparator对 AnagramFamily(字符串列表)进行排序以获得所需的输出。

I think you can use compareTo for this action no ?我认为您可以将 compareTo 用于此操作,不是吗? compareTo from Comparable来自 Comparable 的 compareTo

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

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