The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Finding free records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Finding free records" Watch "Finding free records" New topic

Finding free records

Steve Smith

Joined: Jul 03, 2005
Posts: 26
Hi all,

I have the URLyBIRD assigment. I want to be able to find the first record number in the database that can be used for a new record. My idea is something like this:

Iterate through each record number using read() until a RecordNotFoundException is encountered. Then return the record number that caused the exception. Is it bad practice to write a method in this way so that an exception is always thrown? What might be a better strategy? The reason I wanted to do it like this is so I can reuse my read method.


Thomas Paul Bigbee
Ranch Hand

Joined: Jun 28, 2005
Posts: 71
The RecordNotFoundException can be thrown for several reasons, one the record number to read does not exist, or the record that is trying to be read has been deleted, also your reading a lot of records, when all you really want to do is read a short. You may want to consider doing something along the lines of the below...

Hope this helps
Christian P�cher

Joined: Sep 04, 2005
Posts: 8
I took a BitSet to indicate a used record. It has this nice nextClearBit(int arg) method, which saves me a loop and I expect it to work in O(1) in contrast to O(n) of the linear search you suggested.

I agree. Here's the link:
subject: Finding free records
It's not a secret anymore!