import java.util.StringTokenizer;
class Count {
int count;
String name;
void SetCount(int c, String n) {
this.count = c;
this.name = n;
}
void Show() {
System.out.print("Word= " + name);
System.out.print(" Count= " + count);
System.out.println();
}
}
class Contains2 extends Count {
public static void main(String args[]) {
String s = "Hello this program will repeat itself for this useless purpose and will not end until it repeats itself again and again and again so watch out";
int i, c2, j;
StringTokenizer st = new StringTokenizer(s, " ");
c2 = st.countTokens();
String[] test = new String[c2];
Count[] c = new Count[c2];
for (i = 0; i < c2; i++) {
c[i] = new Count();
}
i = 0;
while (st.hasMoreTokens()) {
String token = st.nextToken();
test[i] = token;
c[i].SetCount(0, test[i]);
i++;
}
for (i = 0; i < c2; i++) {
for (j = 0; j < c2; j++) {
if (c[i].name.equals(test[j]))
c[i].count += 1;
}
}
for (i = 0; i < c2; i++) {
c[i].Show();
}
}
}
私は段落内で繰り返されるすべての単語の数を数えるためにこの小さなプログラムを作成しました。その作業は計画どおりですが、私はそれぞれ別のオブジェクトを作ってそれらをすべて印刷して以来、すべての単語の重複を取得しています。だから私はそれらの名前に基づいてそれらのオブジェクトを削除することを意味する重複する単語を削除することができますどのような方法です。私はそれらをnullに設定することができますが、それでもそれらを取り除くか、何とかそれらをスキップしたいので、それらを印刷します。Javaのオブジェクトの配列からオブジェクトを削除する
私は[HashMapの](https://docs.oracle.com/javase/8/docs/api/java/util/HashMapを使用します.html)。 – Fildor
HashMapはあなたのシナリオに最適ですが、ヌルに設定することができます。ヌルでない場合はコールショーのみを表示します – Sanjeev