| | |
Calling Object Methods within a LinkedList
![]() |
•
•
Join Date: Dec 2004
Posts: 7
Reputation:
Solved Threads: 0
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.
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
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.
Java Syntax (Toggle Plain Text)
public class Item implements Comparable { private int myId; private int myInv; public Item(int id, int inv) { myId = id; myInv = inv; } public int getId() { return myId; } public int getInv() { return myInv; } public int compareTo(Object otherObject) { Item right = (Item)otherObject; return myId - right.myId; } public boolean equals(Object otherObject) { return compareTo(otherObject) == 0; } public String toString() { String string = (getId() + " " + getInv()); return string; } }
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
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:
You'd then declare the List as:
Java Syntax (Toggle Plain Text)
List<Item> myList = new LinkedList<Item>();
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Mar 2004
Posts: 10
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- get/set Methods in java (Java)
- C++ Performance Tips (C++)
- performance benefit by not calling static member function by object (C)
- IllegalStateException? (Java)
- Question about object lock (Java)
- Can you add pictures/sounds in a win32 console app? (C)
Other Threads in the Java Forum
- Previous Thread: Using JNI to find the CPU
- Next Thread: Encryption -- Custom algorithm
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






