0

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

4
  • The way in java is to get result set into a Collection instance, most preferred being ArrayList. You dont need to know the size before hand, for each row you create an object and add it to the list. Commented Mar 6, 2012 at 10:55
  • Array should be used, when size is known to you. You don't know the size, so use any Collection classes as per your requirement Commented Mar 6, 2012 at 10:57
  • Not sure how you are handling your inter-program communication, but you can probably do the same you do with arrays with Lists. Worst case scenario, you transform your List into an array (since the list allows you to see its size). Commented Mar 6, 2012 at 11:09
  • @ggkmath, see the edit in my previous answer. Commented Mar 6, 2012 at 13:02

2 Answers 2

3

Why are you adding it to arrays? You can easily iterate through the ResultSet, transform the results to the appropriate Objects, and add them to an ArrayList... 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 count query before running your original one.

EDIT: From the documentation you linked, it would seem that if you use a Java ArrayList you'd end up with an ActionScript mx.collections.ArrayCollection object instead of the ActionScript Array object you'd get if you used a Java array. Your choice which one to use, just convert List -> array if you can't change your ActionScript code...:

List<MyObject> myList = new ArrayList<MyObject>();
... populate myList from ResultSet ...
MyObject[] array = myList.toArray(new MyObject[myList.size()]);
Sign up to request clarification or add additional context in comments.

1 Comment

I think this is the right answer, but I need more help to fill it in. I created the following more focused thread to address it: stackoverflow.com/questions/9585764/…
0

as what Marcelo said, ArrayList is a better option for this problem, but instead of executing a COUNT query to know how many rows is returned, you can trick it by using:

rs.last(); rs.getRow();

then gat back to the first row after,.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.