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?
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.