aspose file tools*
The moose likes Beginning Java and the fly likes How to save linkedlist to file using object serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to save linkedlist to file using object serialization" Watch "How to save linkedlist to file using object serialization" New topic
Author

How to save linkedlist to file using object serialization

max wang
Greenhorn

Joined: Jan 10, 2008
Posts: 7


linkedlist contain a series of Node containing object which implements Seriazable and its reference. But I am not sure if I can directly save to add.dat using out.writeObject(it.next()), as it.next() is only object, there is no refrence(or pointer like in C++). So How can I use iterator to save the linked list. I appreciate someone coule help me solve this problem.

[ May 14, 2008: Message edited by: max wang ]
[edit]Added code tags CR 15th May 2008[/edit]
[ May 15, 2008: Message edited by: Campbell Ritchie ]
Guido Sautter
Ranch Hand

Joined: Dec 22, 2004
Posts: 142
How about just giving it a try? That's usually the best way of learning and understanding things. If you face any particular problems, just post them here and there will surely be someone to help you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

May I give you an advise? Store the entire LinkedList instead. This will solve a "major" problem in reading back the data - with your solution you don't know when to stop reading, and will encounter a java.io.EOFException. If you store the list itself you can read the list back from the stream and then iterate over the list to get the elements back.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
max wang
Greenhorn

Joined: Jan 10, 2008
Posts: 7
Thanks rob, i change the code and store the entire LinkedList. It works and node can be retrieved well also. But I have another problem. My assignment is desige a AddressList class(main class) which compose of AddressCard (the linked nodes). AddressCard is stored in LinkList. The LinkList has an inner class--LinkListIterator which implements Iterator(interface). I can not used jave predesigned LinkedList.

The requirement ask input a String(Title of AddressCard) and find a AddressCard object which has same title.

The find() I write as belows,

//------------------------find first address--------------------------
public void find()
{
Scanner kb2 = new Scanner(System.in);
System.out.print("please input first name:");
String str=kb2.nextLine();
boolean FIND=false;
AddressCard a=null;

while(lnklst.iterator().hasNext())
{
if(lnklst.iterator().next().getname().startsWith(str))
{
FIND=true;
a=lnklst.iterator().next();
break;
}

}
if(!FIND) System.out.println("NOT FIND");
else System.out.println("You have find the
address:\n"+a);

Compile is ok, but when I run, while(lnklst.iterator().hasNext()) became a deadlock. I am confused why the iterator cann't work. Can you give me a hint. Thanks
[ May 15, 2008: Message edited by: max wang ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
and . . .
Welcome to the Ranch, Max Wang

Please use the code button below the "message" window for quoted code; it makes it easier to read. I have done it on your 1st post. Please do it on your 2nd post: use edit, then highlight the code block then click the CODE button.

Knowing my luck, Rob Prime will give you the answer about 1 minute before I do; he usually does.

Your problem is caused by creating a new Iterator object in the beginning of your while loop. So you are always going back to the beginning and always finding a next element. What you want is . . .

Iterator it = lnklst.iterator();
while (it.hasNext()) . . .

CR
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Congrats Campbell, you beat me to it

You're absolutely right about the iterator too; but Max has made one wrong assumption. There is no deadlock there, just an infinite loop. The difference is that in deadlock, the thread is waiting, while in an infinite loop it is busy indefinitely.
As Campbell has correctly seen, this is caused because you create a new Iterator each time. As such, you always check if the iterator has a first element - which it does.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Originally posted by Rob Prime:
Congrats Campbell, you beat me to it
But I missed the bit about "deadlock."

max wang
Greenhorn

Joined: Jan 10, 2008
Posts: 7
Thanks,


if the AddressList is
Mr.aa -> Mr.bb->Mr.cc->Mr.dd . I want to find the Mr.bb, when I run the above code, the result of println() is Mr.cc, I think the reason is that if(it.next().getname().startsWith(str)) find the Mr.bb, but it.next()inside the method block point the next node so the println has to output Mr.cc. So How can I get the Mr.bb.

[ May 15, 2008: Message edited by: max wang ]

[ May 15, 2008: Message edited by: max wang ]
[ May 15, 2008: Message edited by: max wang ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
The problem with using it.next() twice is that you move onto the next element. You create a reference to it, then you use the reference.

See whether that helps
max wang
Greenhorn

Joined: Jan 10, 2008
Posts: 7
thank you very very much, I change the code follow your method, so easy to solve my difficulty, I sit before the laptop for 3 hours... I never thought the reason is in if(), i should spend much more time to study the literator. Thanks Campbell,rob.
[ May 15, 2008: Message edited by: max wang ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You're welcome
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Originally posted by max wang:

This is just asking for problems. The first time you call it.next(), it moves the iterator one position forward. Then you call it.next() again, and that is most likely not what you want. In fact, it can cause a NoSuchElementException.

Most times you want to call it.next() only once, and cache that value:

Now of course this will not work; you will either have to cast the return of it.next() to something with a getname() method (AddressCard), or use generics with the List and Iterator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to save linkedlist to file using object serialization