Simple Array Dimensioning question

Hey all,

What is the easiest way to dynamically set the array dimension of an array? I have a method that returns an object array of type Project. The array size of the returned object varries depending on what parameters are passed. I do not know how to declare the object that calls the method. Can you help me? Thanks.

DetailedProject[] test =new DetailedProject[5];

test = myBudgetDAO.getProjects("2006","O&M","25","109","98");

[619 byte] By [griffindja] at [2007-10-2 16:04:41]
# 1
You create the array inside the getProjects method don't you?You're not passing it in there, so it can't use the existing one.
evnafetsa at 2007-7-13 16:40:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

So you're saying to use

DetailedProject[] = myBudgetDAO.getProjects("2006","O&M","25","109","98");

Would I still be able to call the same method with different parameters?

Would the array dimensions change depending on the size returned,

say 2006 projects where size of 50 and 2007 projects are size of 75?

Or would I use a different array for the 2007 projects?

griffindja at 2007-7-13 16:40:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

>Would I still be able to call the same method with different parameters?

Yes. All you say is that the method is returning an array.

The size of the array matters not to your main program, just that the method returns an array of the appropriate objects.

Something in your getProjects method you must be declaring/creating an array. Look at that code.

The getProjects method must know how many to create if it is returning the correctly sized array.

An alternative is instead of using an array, to use a List (eg ArrayList) which dynamically resizes itself, and you don't need to set inital capacity.

evnafetsa at 2007-7-13 16:40:41 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...