For Programmers: Free Programming Magazines  


Home > Archive > Java Help > October 2004 > class LinkedList









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author class LinkedList
Heiko Fleischmann

2004-10-27, 8:57 am

Hi!
By using the class LinkedList, I wrote a method, that returns a list of
Elements of the type of a class, I wrote too.
Now I want to access a method of the first Element by using the method
getFirst() (this should return an element of the type of the class in my
opinion) and then the method name. But as the type of the returned
Element is now Object, I can only access the methods of the class Object
and not of the class, I thought my Elements would be a type of.
Thank you.
Paul H. van Rossem

2004-10-27, 3:59 pm

Hi Heiko,

That's because LinkedList only knows how to link Objects. But since you
happen to know that your Element is also an Element, you can cast it
back to an Element:
Element xxx = (Element)list.getFirst();
If you are using JDK 1.5 (J2SE 5.0), you could also have a look at
generics, which offers a much nicer and safer solution.
See http://java.sun.com/developer/techn.../J2SE/generics/
Good luck, Paul.

On 27-10-2004 12:40, Heiko Fleischmann wrote:
> Hi!
> By using the class LinkedList, I wrote a method, that returns a list of
> Elements of the type of a class, I wrote too.
> Now I want to access a method of the first Element by using the method
> getFirst() (this should return an element of the type of the class in my
> opinion) and then the method name. But as the type of the returned
> Element is now Object, I can only access the methods of the class Object
> and not of the class, I thought my Elements would be a type of.
> Thank you.

Heiko Fleischmann

2004-10-27, 3:59 pm

Paul H. van Rossem wrote:
> Hi Heiko,
>
> That's because LinkedList only knows how to link Objects. But since you
> happen to know that your Element is also an Element, you can cast it
> back to an Element:
> Element xxx = (Element)list.getFirst();
> If you are using JDK 1.5 (J2SE 5.0), you could also have a look at
> generics, which offers a much nicer and safer solution.
> See http://java.sun.com/developer/techn.../J2SE/generics/
> Good luck, Paul.
>

Hi Paul!
Thank you for your quick response!
I think I got it now.
Best regards!
Heiko.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com