ファイル内のすべての文字を解析し、各文字の出現回数を見つけるクラスを作成しています。
1 つのステップで、文字を追加するファイルを調べているときに、現在の特定の文字が文字のリストに既に含まれているかどうかを確認します。そうでない場合は、出現値 1 でリストに追加し、既に含まれている場合は、出現値を 1 増やします。
ただし、以下のコードは、リストに特定の文字が既に含まれているかどうかを正しく判断していません。
私は ArrayList を使用していますが、contains() が使用する equals() メソッドを適切に機能させるために、CharProfile クラスの equals メソッドをオーバーライドする必要があることを知っています。
CharProfile の equals メソッドをオーバーライドしました:
@Override
public boolean equals(Object o) {
if (this.character == ((Character)o)) {
return true;
}
else {
return false;
}
}
コード:
import java.util.*;
import java.io.*;
public class HuffmanCoder {
public static void main(String[] args) throws IOException {
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the name of the file to be read from: ");
String fileName = keyboard.nextLine();
File file = new File(fileName);
Scanner inputFile = new Scanner(file);
int charCount = 0;
ArrayList<CharProfile> charOccurrences = new ArrayList<CharProfile>();
while (inputFile.hasNext()) {
// Grabs the next word in the file and converts it into a char array
String word = inputFile.next();
char[] wordArray = word.toCharArray();
// Parses the word on a char-by-char basis
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i] != ' ') {
charCount++;
// Constructs the list of chars and their respective number of occurrences
if (!charOccurrences.contains(wordArray[i])) {
charOccurrences.add(new CharProfile(wordArray[i]));
}
else {
for (int j = 0; j < charOccurrences.size(); j++) {
if (charOccurrences.get(j).getCharacter() == wordArray[i]) {
charOccurrences.get(j).incremementOccurrences();
break;
}
}
}
}
}
}
// Figure out each char's probability
for (int i = 0; i < charOccurrences.size(); i++) {
System.out.println(charOccurrences.get(i).getCharacter());
}
}
}
完全な CharProfile クラス:
public class CharProfile {
private char character;
private int occurrences;
private double probability;
public CharProfile(char character) {
this.character = character;
occurrences = 1;
}
public void incremementOccurrences() {
occurrences++;
}
public char getCharacter() {
return character;
}
public void setCharacter(char character) {
this.character = character;
}
public int getOccurrences() {
return occurrences;
}
public void setOccurrences(int occurrences) {
this.occurrences = occurrences;
}
public double getProbability() {
return probability;
}
public void setProbability(double probability) {
this.probability = probability;
}
public boolean equals(char character) {
if (this.character == character) {
return true;
}
else {
return false;
}
}
@Override
public boolean equals(Object o) {
if (this.character == ((Character)o)) {
return true;
}
else if (this.character == ((Character)o).charValue()) {
return true;
}
else {
return false;
}
}
}
簡単に言えば、リストにその文字が既に含まれているかどうかを確認するときに true を返す代わりに、常に false を返すように見えるため、ファイル内のすべての文字がリストに追加されます。