私はクラスを持っています:
public class WordNode {
private String _word;
private WordNode _next;
....
}
および次のリスト:
public class TextList {
private WordNode _head;
public char mostFrequentStartingLetter(....){}
}
TextList クラスでは、リスト内の単語が始まる最も頻度の高い文字を返す再帰メソッド (mostFrequentStartingLetter) を使用する必要があります...どこから始めればよいかわかりません.....
助けてください...
ありがとう、アロナ
あなたが知っているように、私は不正行為をしていません:
public class TextList {
private WordNode _head;
public TextList(String text) {
String word = "";
WordNode tmp;
// After the split that in the array we are going over all the array
for (int i = 0; i < text.length(); i++) {
for (int j = 0; j < text.length(); j++) {
if (text.charAt(j) == ' ') {
word = text.substring(0, j);
text = text.substring(j + 1);
i = 0;
break;
} else if (j == text.length() - 1) {
word = text.substring(0, j + 1);
text = text.substring(j + 1);
break;
}
}
if (_head == null) {
tmp = new WordNode(word, null);
_head = tmp;
}
// if the word starts with a smalles letter then the head, make it
// the head
else if (_head.getWord().compareTo(word) > 0) {
tmp = new WordNode(word, _head);
_head = tmp;
} else {
WordNode current;
current = _head;
// go over all the nodes in the list and push the current word
// to the list in the right order
while (current.getNext() != null) {
if (current.getWord().compareTo(word) < 1
&& current.getNext().getWord().compareTo(word) > 0) {
tmp = new WordNode(word, current.getNext());
current.setNext(tmp);
break;
}
current = current.getNext();
}
// If the current was the tail, check that the word is bigger
// and then make it the tail.
if (current.getNext() == null
&& current.getWord().compareTo(word) < 1) {
tmp = new WordNode(word, null);
current.setNext(tmp);
}
}
}
}
public String mostFrequentWord() {
String frequentWord = _head.getWord();
WordNode current = _head;
int count = 0;
int max = 0;
while (current.getNext() != null) {
if (current.getWord().compareTo(current.getNext().getWord()) == 0) {
count++;
}
if (count > max) {
max = count; frequentWord = current.getWord();
}
current = current.getNext();
}
return frequentWord;
}
public String toString() {
String s = "";
WordNode current = _head;
int count = 1;
while (current != null) {
while (current.getNext() != null && current.getWord().equals(current.getNext().getWord())) {
count++;
current = current.getNext();
}
s += current.getWord() + "\t" + count + "\n";
count = 1;
current = current.getNext();
}
return s;
}
public char mostFrequentStartingLetter(....){}
}