Calling Object Methods within a LinkedList

Reply

Join Date: Dec 2004
Posts: 7
Reputation: Lord Felix is an unknown quantity at this point 
Solved Threads: 0
Lord Felix Lord Felix is offline Offline
Newbie Poster

Calling Object Methods within a LinkedList

 
0
  #1
Mar 16th, 2005
I need some help in trying to find out how to cast a method that is within an object which is inside a LinkedList.

Let me try to explain:

I already know that I have to use a wrapper to convert an object back into another state. The think I am confused about is how to use a method from the object. What I am doing is storing Objects called Item into a LinkedList. I have a method from Item that gets a stored variable. Heres the code for my Item class.

  1. public class Item implements Comparable
  2. {
  3. private int myId;
  4. private int myInv;
  5.  
  6. public Item(int id, int inv)
  7. {
  8. myId = id;
  9. myInv = inv;
  10. }
  11.  
  12. public int getId()
  13. {
  14. return myId;
  15. }
  16.  
  17. public int getInv()
  18. {
  19. return myInv;
  20. }
  21.  
  22. public int compareTo(Object otherObject)
  23. {
  24. Item right = (Item)otherObject;
  25. return myId - right.myId;
  26. }
  27. public boolean equals(Object otherObject)
  28. {
  29. return compareTo(otherObject) == 0;
  30. }
  31.  
  32. public String toString()
  33. {
  34. String string = (getId() + " " + getInv());
  35.  
  36. return string;
  37. }
  38. }

From my experience so far, Arrays with stored objects only need to method call after referencing it.

int id = myArray[index].getId()

What I tried was:

int id = myList.get(index).getId();

I knew that this aproach probably wouldn't work anyway. So right now I need someone to help me to understand how to call a method from an object within a LinkedList.


Thanks in advanced,

Lord Felix
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,416
Reputation: jwenting is a glorious beacon of light jwenting is a glorious beacon of light jwenting is a glorious beacon of light jwenting is a glorious beacon of light jwenting is a glorious beacon of light jwenting is a glorious beacon of light 
Solved Threads: 231
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Calling Object Methods within a LinkedList

 
0
  #2
Mar 16th, 2005
You need to explicitly cast the object you retrieve from the List to the correct type unless you use a 1.5 level compiler in which case you can use generics to declare the List to only accept a specific type (and thus do the cast for you on retrieval).

You'd then declare the List as:
  1. List<Item> myList = new LinkedList<Item>();
42
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 10
Reputation: Meldroz is an unknown quantity at this point 
Solved Threads: 0
Meldroz Meldroz is offline Offline
Newbie Poster

Re: Calling Object Methods within a LinkedList

 
0
  #3
Mar 18th, 2005
Hey there,

int id = myList.get(index).getId(); // yeah this line could work

The only problem is your storing objects, the list doesn't know at run time what kind of object is stored there i think. You would have cast it like jwenting said.

Item t = (Item)myList.get(index);
int id = t.getId();

This would work i think.

good luck,
Mel
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4026 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC