Click to See Complete Forum and Search --> : how do you use arrays when the size is not known.


Chrisfl
August 18th, 1999, 07:44 PM
How do you use an array to pass to a function when the function is...

OpenPrinter(String, int [], null /* this is always null)

This function fills the int [] array.

How do I pass an array to the second paramter when i don't know the size so that the function can build up the array???

August 18th, 1999, 11:07 PM
In java if u do not know the size to allocate ,Vector(java.uitl package) can be used.The Vector object grows dynamically and no size is required,but still Vector constructor can take size as a parameter if needed.In case if u want to use arrays the size is mandatory and can be taken as input from the user.

SWThomas
August 23rd, 1999, 08:57 PM
Your question is not exactly clear. Are you calling the function, and you want to know how big to make the array? Are you writing the function and you want to know how long the array is so you can fill it? Or something else?

In the first case, if there is some minimum length for the array, the function documentation should specify it.

In the second case, the number of elements in an existing array is given by its length member:


int a = new int[4];
System.out.println("Number of elements in a is " + a.length);