0

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.

1
  • What is br? If that's a BufferedReader, then obviously you read String, and it can't be casted to a Position. Commented May 9, 2012 at 19:57

2 Answers 2

3

You can't arbitrarily cast objects to other types in all cases. Consider writing a constructor for Position that takes the string value(s) the user provides as parameters.

Sign up to request clarification or add additional context in comments.

Comments

2

As the error says you are trying to say that a String is a Position, which it is not. Instead you need to create a Position from the String:

String s = br.readLine();
Position p = new DNode(s);

And you will have to implement the logic of how to convert a String into a DNode object in the DNode class's constructor

2 Comments

I created a constructor in DNode that took string as parameter. But I am not able to set the prev and next fields to anything other than null (which I do not want). What should I do?
You need to add the prev/next links externally by calling say a setNext()/setPrev() method. Who is doing the calling is another question: you could do it directly from main(), but since you have a NodeList class already, you could call its insertNode() method. If you do the latter, you can specify the position via an index or an Iterator (if your List supports that) -- it's up to you, based on what makes sense (see if there is anything similar in the List interface already; if yes, implement/use that method)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.