| | |
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>();
42
•
•
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
Views: 4026 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Java
add android applet application arguments array arraylist arrays binary bluetooth build c# chat class classes client code combobox compiler component convert data database desktop detection draw eclipse error event exception fast file fractal game givemetehcodez graphics gridlayout gui helpwithhomework html ide image images input integer j2me java javafx jframe jmf jpanel jtable jtextfield key linked linked-list list loop main method methods mobile netbeans newbie node number object oracle output parameter pattern phone print printing problem program programming project read remote remove robot scanner screen search server service set size sms socket sort source sql string swing test text transfer tree






