我正在索引文本文件,我想按字母順序打印出文本文件中的每個單詞和頁碼。進出口運行與字母排序,雖然一個問題...這裏是我目前有...Java按字母順序排序
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;
}
}
和documentIndex包含addWord
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);
}
}
}
不在於它解決了什麼,但'i.toUpperCase()。等於(word.toUpperCase())'可以改寫爲'i.equalsIgnoreCase(字)'。 – Pshemo
@ user3808597請更新您的OP中的代碼,以便來自外部的任何人都需要回答您的問題。 –