Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

How to save linkedlist to file using object serialization

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
max wang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Congrats Campbell, you beat me to it

But I missed the bit about "deadlock."

 
max wang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Rob Spoor
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic