是的,这是一个家庭作业项目.
话虽如此,我希望从错误中吸取教训,而不仅仅是让某人为我做错.
我的项目是一个单词频率列表 – 我接受一个文本文件(或网站URL)并计算:
– 唯一单词的数量,和
– 它们出现的次数.
除了一个之外,我提供了所有方法:insert(E word)方法,其中参数是泛型类型的单词.
该单词存储在节点(链接列表项目)中,该节点还具有“计数”值,该值表示单词在正在读取的文本中出现的次数.
该方法必须做的是以下内容:
>如果参数已在列表中,则递增该元素的计数.我做了这个部分
>如果在列表中找不到参数,请将其附加到列表中.我也做过这一部分.
>按降序计数值对列表进行排序.即最高 – >最低计数
3.5.如果两个元素具有相同的计数值,则它们按其单词的字典顺序排序.
我对链接列表非常不熟悉,因此我遇到了很多NullPointerExceptions.这是我目前的插入方法:
public void insert(E word){
if(word.equals("")){
return;
}
if(first == null){//if list is null (no elements)
/*Node item = new Node(word);
first = item;*/
first = new Node(word);
}
else{//first != null
Node itemToAdd = new Node(word);
boolean inList = false;
for(Node x = first; x != null; x=x.next){
if (x.key.equals(word)){// if word is found in list
x.count++;//incr
inList = true;//found in list
break;//get out of for
}//end IF
if(x.next == null && inList == false){//if end of list && not found
x.next = itemToAdd;//add to end of list
break;
}//end IF
}//end FOR
//EVERYTHING ABOVE THIS LINE WORKS.
if (!isSorted()){
countSort();
}
}//end ELSE
}//end method
我的isSorted()方法:
public boolean isSorted(){
for(Node copy = first; copy.next != null; copy = copy.next){
if (copy.count < copy.next.count){
return false;
}
}
return true;
}
最后但并非最不重要的是,我正在努力的部分,排序方法:
public void countSort(){
for (Node x = first, p = x.next; p != null; x=x.next, p=p.next){
// x will start at the first Node, P will always be 1 node ahead of X.
if(x == first && (x.count < p.count)){
Node oldfirst = first;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
if (x.count < p.count){
//copy.next == x.
Node oldfirst = first;
oldfirst.next = first.next;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
if (x.count == p.count){
if(x.toString().charAt(0) < p.toString().charAt(0)){
//[x]->[p]->[q]
Node oldfirst = first;
x.next = p.next;
first = p;
first.next = oldfirst;
break;
}
}
}
}
这是我给出的类/方法调用时insert方法的输出:
Elapsed time:0.084
(the,60)
(of,49)
(a,39)
(is,46)
(to,36)
(and,31)
(can,9)
(in,19)
(more,7)
(thing,7)
(violent,3)
(things,3)
(from,9)
(collected,1)
(quotes,1)
(albert,1)
(einstein,2)
(any,2)
(intelligent,1)
(fool,1)
(make,1)
(bigger,1)
(complex,1)
(it,11)
(takes,1)
(touch,1)
(genius,1)
(lot,1)
(courage,1)
(move,1)
(opposite,1)
(direction,1)
(imagination,1)
(important,5)
(than,3)
(knowledge,3)
(gravitation,1)
(not,17)
(responsible,1)
(for,14)
(people,2)
(falling,1)
(love,2)
(i,13)
(want,1)
(know,3)
(god,4)
(s,8)
(thoughts,2)
(rest,2)
(are,11)
(details,2)
(hardest,1)
(world,7)
(understand,3)
(income,1)
(tax,1)
(reality,3)
(merely,1)
(an,7)
(illusion,2)
(albeit,1)
(very,3)
(persistent,2)
(one,12)
(only,7)
(real,1)
(valuable,1)
(intuition,1)
(person,1)
(starts,1)
(live,2)
(when,3)
(he,11)
(outside,1)
(himself,4)
(am,1)
(convinced,1)
(that,14)
(does,5)
(play,2)
(dice,1)
(subtle,1)
(but,8)
(malicious,1)
(weakness,2)
(attitude,1)
(becomes,1)
(character,1)
(never,3)
(think,1)
(future,2)
(comes,1)
(soon,1)
(enough,1)
(eternal,1)
(mystery,1)
(its,4)
(comprehensibility,1)
(sometimes,1)
我最初的想法是尝试循环if(!isSorted()){countSort();}部分只是重复运行直到它被排序,但我似乎在这样做时遇到无限循环.我试过跟随我教授的讲义,但不幸的是他两次发布了前一讲的笔记,所以我很茫然.
我不确定它是否值得一提,但它们为我提供了一个方法hasNext()和next()的迭代器 – 我怎么能用它?我无法想象如果它没用就会提供它.
我哪里错了?
解决方法:
你很亲密首先,比较项目的功能不完整,因此isSorted()可能会产生错误的结果(如果计数相同但单词的顺序错误).这也用于排序,因此最好提取一个比较方法:
// returns a value < 0 if a < b, a value > 0 if a > b and 0 if a == b
public int compare(Node a, Node b) {
if (a.count == b.count)
return a.word.compareTo(b.word);
// case-insensitive: a.word.toLoweCase().compareTo(b.word.toLowerCase())
} else {
return a.count - b.count;
}
}
或简化,在您的情况下足够:
public boolean correctOrder(Node a, Node b) {
if (a.count > b.count)
return true;
else if (a.count < b.count)
return false;
else
return a.word.compareTo(b.word) <= 0;
}
对于您似乎选择冒泡排序的排序,但您缺少外部部分:
boolean change;
do {
change = false;
Node oldX = null;
// your for:
for (Node x = first; x.next != null; x = x.next) {
if (!correctOrder(x, x.next)) {
// swap x and x.next, if oldX == null then x == first
change = true;
}
oldX = x;
}
} while (change);
我们可以使用Java本机库实现的帮助或更有效的排序算法,但从练习来看,排序算法的性能尚无关注,首先需要掌握基本概念.
标签:java,sorting,list
来源: https://codeday.me/bug/20190708/1400143.html