Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding free records

 
Steve Smith
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers,

Steve.
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

chris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic