Das Projekt, an dem ich gerade arbeite, beinhaltet das Lesen von Wörtern aus einer Textdatei und das Laden von Wörtern in ein Array (und eventuell einen Binärbaum, der aber später beendet wird). Ich muss sowohl die Wort- als auch die Worthäufigkeit (anfangs 1) in das Array laden, also habe ich beide Variablen in ein Objekt WordNode
gepackt. Ich kann die Wörter in das Array laden, aber die Dinge fallen auseinander, wenn ich versuche zu überprüfen, ob ein Wort bereits im Array ist. Wenn dies der Fall ist, muss ich die Häufigkeit um 1 erhöhen. Ich habe eine neue equals()
-Methode geschrieben, um die Standardmethode zu überschreiben, so dass Wörter anstelle von Variablenreferenzen verglichen werden. Allerdings bekomme ich jetzt den Fehler Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [LWordNode;
. Warum kommt das auf? Ich dachte nur WordNode
Objekte wurden an equals()
übergeben.Java Class Cast Ausnahme
Main-Methode:
public class Driver {
/////////////// fields ///////////////
public static ArrayUnorderedList<WordNode> wordArray = new ArrayUnorderedList<WordNode>();
public static LinkedBinarySearchTree<WordNode> wordTree = new LinkedBinarySearchTree<WordNode>(); //tree to hold words
/////////////// methods ///////////////
public static void main(String[] args) throws Exception {
//ask for filename
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the file to read from: ");
Reader file = new FileReader(reader.readLine());
//read file
Scanner input = new Scanner(file);
while(input.hasNext()) {
//get words from file
String word = input.next();
//remove non-word characters and convert to lowercase
word = word.replaceAll("\\W", "");
word = word.toLowerCase();
//create node
WordNode newWord = new WordNode(word);
//if word is already in array
if(wordArray.contains(newWord)) {
System.out.println("Word is already in array");
//increment frequency by 1
int index = wordArray.find(newWord);
wordArray.list[index].setFrequency(wordArray.list[index].getFrequency() + 1);
System.out.println("(" + newWord.getFrequency() + ") " + newWord.getWord());
} else {
System.out.println("Word is not yet in array");
//add word to tree
System.out.println("(" + newWord.getFrequency() + ") " + newWord.getWord());
wordArray.addToRear(newWord);
}
}
//insert into tree
//perform traversals on tree
}
WordNode Klasse:
public class WordNode {
protected String word;
protected WordNode left, right;
protected int frequency;
/**
* Creates a new node with the specified data.
* @param obj the element that will become a part of the new node
*/
WordNode(String obj) {
word = obj;
left = null;
right = null;
frequency = 1;
}
/**
* Method to check for string equivalence.
* @return true if words are the same
*/
public boolean equals(Object obj) {
WordNode tempWord = (WordNode)obj;
return(tempWord.getWord().equals(this.word));
}
/**
* Gets the word.
* @return the word
*/
public String getWord() {
return word;
}
/**
* Sets the word.
* @param word the word to set
*/
public void setWord(String word) {
this.word = word;
}
/**
* Gets the left.
* @return the left
*/
public WordNode getLeft() {
return left;
}
/**
* Sets the left.
* @param left the left to set
*/
public void setLeft(WordNode left) {
this.left = left;
}
/**
* Gets the right.
* @return the right
*/
public WordNode getRight() {
return right;
}
/**
* Sets the right.
* @param right the right to set
*/
public void setRight(WordNode right) {
this.right = right;
}
/**
* Gets the frequency.
* @return the frequency
*/
public int getFrequency() {
return frequency;
}
/**
* Sets the frequency.
* @param frequency the frequency to set
*/
public void setFrequency(int frequency) {
this.frequency = frequency;
}
}
Einige Methoden aus der Klasse Arraylist:
/**
* Returns true if this list contains the specified element.
* @param target the element that the list is searched for
* @return true if the target is in the list, false if otherwise
*/
public boolean contains(T target) {
return (find(target) != NOT_FOUND);
}
/**
* Returns the array index of the specified element, or the
* constant NOT_FOUND if it is not found.
* @param target the element that the list will be searched for
* @return the integer index into the array containing the target element, or the NOT_FOUND constant
*/
public int find(T target) {
int scan = 0, result = NOT_FOUND;
boolean found = false;
if (!isEmpty()) {
while (!found && scan < rear) {
if (target.equals(list[scan])) {
found = true;
} else {
scan++;
}
}
}
if (found) {
result = scan;
}
return result;
}
Der Fehler ist irgendwo in der 'ArrayUnorderedList' Klasse. Ein Stacktrace und der vollständige Code dieser Klasse wäre hilfreich. – trutheality
Wenn Sie eine Ausnahme erhalten, meldet der Stack-Trace die Zeile, in der die Ausnahme ausgelöst wurde, wodurch der Teil des Codes reduziert wird, den Sie untersuchen müssen. – assylias
Dies ist "zu lokalisiert" und SO ist keine "Code-Review-Site", aber ** es ist wie jedes andere CCE **: Sie versuchen, einen inkonvertierbaren Typ zu erzeugen. Casting * kann * und * nicht * einen Typ ändern. Es ändert nur die "statisch typisierte Ansicht" eines gegebenen Objekts. Bitte benutzen Sie auch die verfügbaren Tools zum Debuggen :( –