I’m new to java but my experience with Matlab and C trained me to ALWAYS pre-allocate memory for an array variable before filling that variable inside a loop (e.g. For loop, While loop, etc).
I find myself retrieving data from a database in Java using ResultSet. A quick search shows there’s no way to obtain the number of rows in the ResultSet without stepping through it. Thus the basic question: how to pre-allocate array length of a Java variable intended to store the results of the ResultSet query, without knowing the number of rows in that ResultSet?
What’s the conventional wisdom? For example, if the ResultSet contains two columns of data, how to place each column into an separate Java array variable?
UPDATE 1: Some background — I need to place everything returned by the ResultSet into an object so that I may pass that object to a non-Java (e.g. ActionScript) program that communicates with the Java program via this object’s contents.
UPDATE 2: Here’s the documentation on the conversion rules from Java to non-Java (e.g. ActionScript). Perhaps
http://help.adobe.com/en_US/LiveCycleDataServicesES/3.1/Developing/WSc3ff6d0ea77859461172e0811f00f6eab8-7ffdUpdate.html
Why are you adding it to arrays? You can easily iterate through the
ResultSet, transform the results to the appropriateObjects, and add them to anArrayList… gives you much more flexibility than adding to an array.But if you really need the number of rows, I think you’ll need to run a
countquery before running your original one.EDIT: From the documentation you linked, it would seem that if you use a Java
ArrayListyou’d end up with an ActionScriptmx.collections.ArrayCollectionobject instead of the ActionScriptArrayobject you’d get if you used a Java array. Your choice which one to use, just convertList -> arrayif you can’t change your ActionScript code…: