Home arrow static arrow Java Programming [Archive] - Add a class into LinkedList
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Add a class into LinkedList
This topic has 7 replies on 1 page.

Posts:37
Registered: 7/31/04
Add a class into LinkedList  
Aug 4, 2004 9:17 AM



 
when i want to add object to linkedlist i use

LinkList.add(MyObject)

its succed but when i want to get it by use this fuction

public MyObject GetIt ( ) {

return this.removeFirst();
}

its give me uncompataple format;

how i can get this object after i insert it in linked ist by using function ;
 

Posts:27,518
Registered: 11/3/97
Re: Add a class into LinkedList  
Aug 4, 2004 9:19 AM (reply 1 of 7)



 
It returns Object, you must cast it to your type.

return (MyObject)this.removeFirst();
 

Posts:5,965
Registered: 5/17/03
Re: Add a class into LinkedList  
Aug 4, 2004 9:23 AM (reply 2 of 7)



 
RemoveFirst returns and Object. You have to cast it to the proper type which is MyObject,

return (MyObject)this.removeFirst();
 

Posts:5,904
Registered: 04/03/99
Re: Add a class into LinkedList  
Aug 4, 2004 9:23 AM (reply 3 of 7)



 
Because the List doesn't know about your class (because it was written before you wrote MyObject) it instead manipulates Object references. Since everything is an Object (by inheritance) that's always valid when passing the reference in.

That is to say, a reference to a MyObject IS a reference to an Object. So...
list.add(myObject);

...is always guaranteed to work. On the other hand, the reverse is not true. An Object IS NOT ALWAYS a MyObject. So when you get it out, it might not work.

You have to "cast" (tell the compiler that the particular Object that you're getting a reference to IS a MyObject) the reference thus...
return (MyObject)list.removeFirst();

...which will now work in your code.

Caution - you don't understand the difference between Objects, references, types and classes (or you would not have asked this question). It is impossible to program in Java without learning this, so the sooner you do so the sooner you will be competent and comfortable. I recommend revising these distinctions in a good learner book on Java.

Dave.
 

Posts:37
Registered: 7/31/04
Re: Add a class into LinkedList  
Aug 4, 2004 9:23 AM (reply 4 of 7)



 
thanx you jschell
 

Posts:19,725
Registered: 9/26/01
Re: Add a class into LinkedList  
Aug 4, 2004 9:39 AM (reply 5 of 7)



 
thanx you jschell
... but kiss off, the rest of you *******! ;-)
(Ok, I know you just hadn't seen their replies at the time you were posting your thanks to jschell)
 

Posts:3,258
Registered: 00-08-28
Re: Add a class into LinkedList  
Aug 4, 2004 9:50 AM (reply 6 of 7)



 
thanx you jschell
... but kiss off, the rest of you *******! ;-)
(Ok, I know you just hadn't seen their replies at the
time you were posting your thanks to jschell)

quite a coincidence
 

Posts:37
Registered: 7/31/04
Re: Add a class into LinkedList  
Aug 4, 2004 6:59 PM (reply 7 of 7)



 
sorry .. dcminter im not see your replay until now. than xfor your replay and for your advice .. im still very juniour on learninng java... thank you again
 
This topic has 7 replies on 1 page.