Click to See Complete Forum and Search --> : using an int [] array when the size is not known


Chrisfl
August 19th, 1999, 08:10 AM
I need to use and declare a int [] array to pass to this function...

something like

int phPrinter[] = ???????

and pass this phPrinter to this function... (the 2nd Parameter)

OpenPrinter(String, int [], Printer_Defaults /* can be null */);

The function initialises the int [] .

Please does anybody know how to do this...

I keep getting an error 87 (invalid parameters) when I try:

int phPrinter[] = {0}; and pass that.

the first param is just a string and the last (3rd) can be null so that leaves param 2... So how does one pass and unknown int array to a function. Class Vector doesn't do what I need to do or does it??

Thanking you in advance.

Chris

unicman
August 22nd, 1999, 10:37 AM
I didn't get the problem completely. But if u want to initialize the variable phPrinter, u can try...

phPrinter = new int[] { 0 };

- UnicMan

August 27th, 1999, 01:40 PM
Hai,
Let me tell u ur problem , u have a array , u wanted to pass it to a function f1 , and there u initialize it right

see in main code
{

// if u declare an array , in java it is treated as an object so it need to initialized to something so make the reference as null
then pass the reference to the function f1*/

int[] a=null;

f1(a);


in function definition:
void f1(int[] a)
{
a=new int[10];
}

All the best
bye
Asha

September 3rd, 1999, 02:55 PM
Yes, a java.util.Vector will do what you need.

Whenever you need an array of unknown dimensions, think Vector.

September 5th, 1999, 10:31 PM
The suggested approach
int [] a = null;
f1(x);
won't work. In Java references are passed by copying their values,
so f1() gets a !copy! of x pointing at nothing.
f1() can assign the (copy of) x to anything with no impact on x itself.

If you're stuck with an array, the option #1 is to make your array
a method's return value:
int x[] = f1(...);
The option #2 assumes that you know in advance the # of elements,
so you can do allocation upfront, e.g.:
int [] a = new int[100];
f1(x);
The option #3 is to wrap your array into an object, e.g.:
class x_wrapper {
public int [] a;
}
and then

x_wrapper a_w = new x_wrapper();
f1(a_w);


In both approaches #2 and #3 f1() will have a reference to smth allocated
in the caller's space, so f1()'s modifications to the object refered by x
will be visible by the caller.

Of cource, using Vector will always work, but for the same reason it
needs to be initialized before being passed to any method:

Vector a = new Vector(...);
f1(a);