Click to See Complete Forum and Search --> : Java - LinkedList.get problems


mart_man00
08-24-2004, 03:55 PM
Im trying to get a linked list created, and then check out every object in it.

Heres what im dealing with


public static void main(String[] args) {
card current = new card(0,0);

int x = 1;
List hand = new LinkedList();

while(x < 10) {
hand.add(new card(x,x));

++x;
}

while(x > 0) {
current = hand.get(x);

--x;
}


}


and i get back


Main.java [30:1] incompatible types
found : java.lang.Object
required: card
current = hand.get(x);
^
1 error
Errors compiling Main.


Thanks

thaddaeus
08-24-2004, 05:16 PM
make sure your passing the correct objects, your passing a standard object to a card object, change any card objects outside the main into objects, i'm betting its in the add function i'd have to see all the code for the entire program (well anything being used in the main class that has the problem)

fiske
08-24-2004, 06:00 PM
Don't know if this would cause the error but for

List hand = new LinkedList();

shouldn't it be:
LinkedList hand = new LinkedList(); ?

fatTrav
08-24-2004, 06:41 PM
List ll = new LinkedList() and LinkedList lll = new LinkedList() both compiled just fine on my machine. You do need to import java.util.LinkedList though (or just java.util.*).

And a bit of FYI, the standard LinkedList is NOT synchronized.

stoe
08-24-2004, 07:28 PM
replace


current = hand.get(x);


with


current = (card) hand.get(x);


You must cast the return value from get() to the desired type, since get() returns type Object.

oldaren
08-25-2004, 10:37 AM
Also notice that the get() method expects a zero based index. If you add() 10 objects, you should use get(9), ... get(0) to fetch them back. If you execute get(10) you will most probably get an ArrayIndexOfOutBoundsException.