File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes findByCriteria returns long[] 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 "findByCriteria returns long[]" Watch "findByCriteria returns long[]" New topic

findByCriteria returns long[]

Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
I am doing B&S assignment and trying to implement findByCriteria here to search records in data file. My question is how do I know before have what the length of long[] is going to be?
I have couple of options to do this:
1) Loop through the data file, find matching records and store them in collection and at the end of method convert collection to array and return an array. I looked at arraylist and it stores only Object and has toArray method which converts it to Object[], in that case how do I implement this?
2) Loop through the database, store it in an array of boolean (where array length will be num of records in DB), if matching record found store true in array else false, keep count of number of trues encountered. Set long[] to that length and loop through boolean array to find which ones are true. This probably isnt the good way is it?

Any help on this is appreciated, I tried searching this forum but could not find much discussion on this.

Thanks in advance.

SCJP 1.4<br />SCWCD 1.3
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
I would opt for choice number one. Store the values in an ArrayList, and use the toArray() method. Then loop through the Object array and fill up a long array.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Jay Ashar
Ranch Hand

Joined: Oct 13, 2002
Posts: 208
Thanks a lot Paul.
I agree. Here's the link:
subject: findByCriteria returns long[]
It's not a secret anymore!