I am trying to implement a list ADT in Java.I followed a book on Data Structures by Goodrich.
I created 2 interfaces called Position:
public interface Position {
Object element()throws InvalidPositionException;
}
and List which has method declaration of lists like first(), last(), etc.
Now, i have a class called DNode.java which implements Position and has methods to assign the previous and next elements for a particular node.
And finally, I have NodeList.java which implements the List interface.
In this, there is a insertBefore() method that takes Position and Object as input and makes changes to the list to insert the node before a particular position.
Now, this method is called from the ListMain class which is the main class.
Object p = (Object) br.readLine();
nl.insertAfter((Position)p, element);
nl.display();
Basically, I have taken an input from the user in the form of a string, converted it to type Object and then converted it to type Position. When I do so, I get a class cast exception that says
java.lang.String cannot be cast to ListPackage.Position
Can someone please tell me how to do the appropriate type casting. Or is my approach wrong and should I be doing something else?
I am new to data structures. So, any help will be appreciated. Thank you very much in advance.
br? If that's a BufferedReader, then obviously you read String, and it can't be casted to aPosition.