Java按字母排序

时间:2015-04-30 22:02:08

标签: java string sorting

我正致力于索引文本文件,我想按字母顺序打印文本文件和页码中的每个单词。我遇到了按字母顺序排序的问题但是...这就是我现在所拥有的......

public void addWord(String word, int num) {
    boolean match = false;
    for (IndexEntry x : this) {
        String i = x.getWord();
        if (i.toUpperCase().equals(word.toUpperCase())) {
            x.add(num);
            match = true;
        }
    }
    if (match == false) {
        IndexEntry entry = new IndexEntry(word);
        int add = 0;
        int count = 0;
        boolean spot = false;
        while (count < this.size() && !spot) {
            String str = this.get(count).getWord();
            if (str.compareTo(word) > 0) {
                add = count;
                spot = true;
            }
            count++;
        }
        this.add(add, entry);
        this.get(indexOf(entry)).add(num);
    }
}

并且输出是......

BLUE[5, 8]
BLACK[7]
NEW[11]
OLD[10]
RED[4]
TWO[2]
FISH[1, 2, 4, 5, 7, 8, 10, 11]
ONE[1]
Done.

这显然不是按字母顺序排列......对此的任何帮助都将非常感激。谢谢。

这里是indexEntry

import java.util.List;
import java.util.ArrayList;

public class IndexEntry implements Comparable<IndexEntry>
{
  private String word;
  private List<Integer> numsList;  // contains Integer objects

  /**
   *  Constructs an IndexEntry for a given word
   *  (converted to upper case); stores the word and
   *  creates an empty ArrayList<Integer> for numsList
   *  @param aWord the word for this entry
   */ 
  public IndexEntry(String aWord)
  {
     word = aWord.toUpperCase();
     numsList = new ArrayList<Integer>();
  }

  /**
   *  Returns word of this IndexEntry object
   *  @return this entry's word
   */
  public String getWord()
  {
    return word;
  }

  /**
   *  Adds num at the end of this IndexEntry's numsList if
   *  num is not already in the list; otherwise makes no changes.
   */
  public void add(int num)
  {
    if(numsList.contains(num) == false)
      numsList.add(num); 
  }

  /**
   *  Compares this entry for equality to another IndexEntry;
   *  the entries are considered equal if their words are
   *  the same
   *  @param obj the other IndexEntry to be compared
   *  @return true if the words match, otherwise false
   */
  public boolean equals(IndexEntry obj)
  {
    if(word.equals(obj.getWord()))
      return true;
    return false;
  }

  /**
   *  Compares this entry to another IndexEntry
   *  by comparing their words
   *  @param obj the other IndexEntry to be compared
   *  @return negative if 'this' entry smaller, 0 if equal, positive is 'this' larger
   */
  public int compareTo(IndexEntry obj)
  {
    return obj.getWord().compareTo(word);
  }

  /**
   *  Converts this IndexEntry into a string
   *  @return the String representation of this entry: word and line numbers
   */
  public String toString()
  {
    return word + numsList;
  }
}

和包含addWord的documentIndex

import java.util.StringTokenizer;

public class DocumentIndex extends java.util.ArrayList<IndexEntry>
{

  /**
   *  Creates an empty DocumentIndex with the default
   *  initial capacity
   */
  public DocumentIndex()
  {
    super();
  }

  /**
   *  Creates an empty DocumentIndex with the capacity
   *  given by the parameter
   *  @param init the initial capacity of the list
   */
  public DocumentIndex(int init)
  {
    super(init);
  }

 /**
  *  If word is in this DocumentIndex and num is in its list, does nothing; 
  *  if word is in this DocumentIndex and num is not in its list, adds num 
  *  to this word's IndexEntry; otherwise creates a new entry with word and
  *  num and inserts it into this index in order
  *  @param word the word to look for
  *  @param num the line number this word is on
  */

  public void addWord( String word, int num )
    {
        boolean match = false;
        for ( IndexEntry x : this ){
            String i = x.getWord();
            if (i.toUpperCase().equals(word.toUpperCase())){
                x.add(num);
                match = true;}}
        if (match == false){
            IndexEntry entry = new IndexEntry(word);
            int add = 0;
            int count = 0;
            boolean spot = false;
            while (count < this.size() && !spot){
                String str = this.get(count).getWord();
                if (str.compareTo(word) > 0){
                    add = count;
                    spot = true;}
                count++;}
            this.add(add, entry);
            this.get(indexOf(entry)).add(num);}
    }

  /**
   *  For each word found in str, calls addWord(word, num)
   *  @param str a line of text
   *  @param num the line number for this line of text
   */
  public void addAllWords(String str, int num)
  {
    StringTokenizer tokens = new StringTokenizer(str, " .,-;?!");
           // " .,-;?!" lists delimeters that separate words 

    while(tokens.hasMoreTokens())
    {
      String word = tokens.nextToken();
      addWord(word, num);
    }
  }
}

2 个答案:

答案 0 :(得分:1)

编辑:您需要在addWord中的while循环后添加以下行:

if ( !spot && (count == this.size())){
    add = count;
}

在我结束时尝试修复错误。

另外,我认为以下版本是编写addWord()方法的更简洁,更有效的方法:

public void addWord( String word, int num ) {
    String upperCaseWord = word.toUpperCase();

    for ( IndexEntry x : this ) {
        String i = x.getWord();
        if (i.equals(upperCaseWord)){
            x.add(num);
            return;
        }
    }

    IndexEntry entry = new IndexEntry(word);
    entry.add(num);

    int currSize = this.size();     
    if (currSize == 0) {
        this.add(entry);
        return;
    }   

    int count = 0;
    while (count < currSize) {
        String str = this.get(count).getWord();
        if (str.compareTo(upperCaseWord) > 0){
            break;
        }   

        count++;
    }   

    this.add(count, entry);
}   

答案 1 :(得分:0)

Java的字符串通常根据其Unicode代码点的数字顺序排序,这不是您想要的。使用Collator按字母顺序排序。