-2

Thought I'll first outline the classes I'm working with while constructing a class that holds a doubly-linked list (I'm not using the Java inbuilt class):

    class Node { //Forms the nodes of the list
        private Object element;
        private Node next;
        private Node prev;  //....other methods etc.
}

    public class mylist implements Iterable<Node> {
     Node head; Node tail; //....
    @Override
        public Iterator<Node> iterator() {
            return new myit(this);
        }
    public class myit implements Iterator<Node> {       
            mylist set; 
            Node curr;
            myit(mylist input) {
                set=input;
                curr=set.head;
            }
            @Override
                public boolean hasNext() {
                    return (!(curr.getNext()==set.tail));
                }
            @Override
            public Node next() {
                if (this.hasNext()) return curr.getNext();
            }

        }



    public class Myset {
            mylist set;
            //...
public void Delete(Object o) {

        for(Node p: this.set) {
            if(p.getElement().equals(o)) {
            Node q=p.getNext();
            p.getPrev().setNext(q);
            q.setPrev(p);
            break;
        }}
    }
    public static void main(String[] as) {
            Myset m=new Myset();
            for(int i=1; i<5;i++) {
                m.Insert(new Card(i));
            }
            for(Node n: m.set){
                Card x=n.getElement(); //ERROR HERE
                System.out.println(x.number());
            }}

To be clear, all the classes here are defined in separate files. Card is a separate class that I've created. I get an error which says Type mismatch: cannot convert from Object to Card in the line indicated. How do I overcome this? Also, as you might have noticed I've overloaded the iterator function to use it for my linked list class using the ways suggested in How to implement iterator as an attribute of a class in Java. If you notice glitches/have any suggestions I'd be extremely grateful. I also had a question related to the Delete method in Myset. I haven't been able to run the code due to the error above yet, but I have my doubts about this. I'm rather new to Java and I'm used to the idea of working with pointers in C. So do p and q serve as 'pointers' to the memory locations of the original nodes in mylist and would this successfully delete the node?

4
  • "How do I overcome this?" You learn about generics. Or you go old-school and cast the value. Commented Jul 3, 2016 at 15:22
  • Node<T> .. T element Commented Jul 3, 2016 at 15:22
  • 1
    Node.getElement() returns Object. You're trying to assign the object returned to a variable of type Card. So the compiler disagrees: it can't be sure that the object is a Card. So you need to cast: Card x = (Card) n.getElement();. Your code is awfully indented, and your variable names are horrible, which makes your code very hard to read, even for you. You would have a much better idea of what your own code does if you formatted it correctly and chose good names. Methods start with a lowercase letter. Classes start with an uppercase letter. camelCase is used. Commented Jul 3, 2016 at 15:24
  • Thank you very much. That solved the issue. I'm awfully sorry about the formatting, I'd pasted it and converted it to code in fragments and I realize it looks pretty darned disjointed to say the least. Commented Jul 3, 2016 at 16:03

1 Answer 1

0

As suggested in comments, one quick and easy solution is to simply cast the return value of getElement() to Card:

Card x = (Card) n.getElement();

The compiler doesn't know that you're only storing Card objects in the list, since it can hold any subclass of Object (or Object itself). In your case, you as the programmer have to tell the compiler what you're doing - and if you are 100% certain that the list will only contain Card objects you can safely cast the return value to Card.

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

Comments

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.