Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Store Object in Linkedlist

Status
Not open for further replies.

domrongpon

Programmer
Nov 17, 2001
2
TH
My problem is about Linkedlist class,i want to create a link list and store an instance to it so i use
//My Java code
LinkedList list = new LinkedList(); // i declare new link list
ProcessBehavior pb=new ProcessBehavior(); // it is my instance
list.add(pb); // i add my instance to my link list
//
What's happen!!? i think pb is stored in list but i cannot get it from my linklist.When i get it by use method getFirst(), it return ProcessBehavior@744da14 to me.
**** Help me,please.
 
You need to cast it to your object type, in this case it would be ProcessBehavior.
 
Thank you wushutwist.But Would it be possible,if you give me an example code about linklist. :>
 
Quick Example using a String object:
Code:
Collection c = new LinkedList();
String test = "test";
c.add(test);

/* 
 * Proper way to loop thru Collections is to 
 * use an Iterator 
 */
for (Iterator ite = c.iterator();ite.hasNext();) {
  /* Get next element and cast to String */
  String data = (String)ite.next();
  System.out.println(data);
}
Notes about example:
Collection is the Super-Interface of LinkedList and should be used instead of returning or referencing a LinkedList directly. If in the future you want to change to something like an ArrayList it will only require changing one line of code.

Let me know if you have any questions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top