Passing arrays as parameters problems...

H

harry

I have 2 multi-dim arrays

double[][] subTotals = null;
String[][] rowTitles = null;

I want to pass them to a function that initialises & populates them like
so -

loadData( rowTitles, subTotals);

this function contains lines like -

subTotals = new double[15][4];
rowTitles = new String[15][9];

They are then populated but when the funtion exits both arrays are back to
null.

I understand (well think I do) the difference between primitive types &
objects as parameters & thought because this would work or at the very least
the rowTitles array would as it's String's

Any ideas?

thanks

harry
 
J

Joona I Palaste

harry said:
I have 2 multi-dim arrays
double[][] subTotals = null;
String[][] rowTitles = null;
I want to pass them to a function that initialises & populates them like
so -
loadData( rowTitles, subTotals);
this function contains lines like -
subTotals = new double[15][4];
rowTitles = new String[15][9];
They are then populated but when the funtion exits both arrays are back to
null.
I understand (well think I do) the difference between primitive types &
objects as parameters & thought because this would work or at the very least
the rowTitles array would as it's String's

Java parameter passing is pass-by-value. Reassigning the parameters
inside method does not affect their values outside the method. You'll
have to initialise your arrays outside loadData() and only do the
population in loadData().

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
A

au714

All parameters to methods are passed by value. The object references
'subTotals' and 'rowTitles' in the method loadData() are copies of the
references in the caller.

So if you do something like rowTitles = new String[15][9] within
loadData(), this changes what the object reference rowTitles refers to
within the method loadData(), but this is not visible in the caller.
This is why rowTitles is unchanged when you return from loadData().

FGB
 
A

Andrew McDonagh

Joona said:
harry said:
I have 2 multi-dim arrays

double[][] subTotals = null;
String[][] rowTitles = null;

I want to pass them to a function that initialises & populates them like
so -

loadData( rowTitles, subTotals);

this function contains lines like -

subTotals = new double[15][4];
rowTitles = new String[15][9];

They are then populated but when the funtion exits both arrays are back to
null.

I understand (well think I do) the difference between primitive types &
objects as parameters & thought because this would work or at the very least
the rowTitles array would as it's String's


Java parameter passing is pass-by-value. Reassigning the parameters
inside method does not affect their values outside the method. You'll
have to initialise your arrays outside loadData() and only do the
population in loadData().

Actually the references are passed by value, but the object they point
to is still accessible as if if was passed by reference. Therefore,
whilst you can't make the original reference point to a new object, you
can change the contents of the object, that the references point too.



public void testSettingArrays() {

Holder holder = new Holder();

MultiDimensionalArrays arrays = new MultiDimensionalArrays();

arrays .fillArrays(holder);

assertNotNull(holder.strings);
assertNotNull(holder.doubles);

}


public class MultiDimensionalArrays {

public void loadData(String[][] strings, Double[][] doubles) {
//won't work
}

public void loadData(Holder holder) {
holder.strings = new String[10][10];
holder.doubles = new Double[20][20];
}

}


class Holder {
String[][] strings = null;
Double[][] doubles = null;
}
 
J

Joona I Palaste

Andrew McDonagh said:
Joona said:
harry said:
I have 2 multi-dim arrays
double[][] subTotals = null;
String[][] rowTitles = null;
I want to pass them to a function that initialises & populates them like
so -
loadData( rowTitles, subTotals);
this function contains lines like -
subTotals = new double[15][4];
rowTitles = new String[15][9];

They are then populated but when the funtion exits both arrays are back to
null.
I understand (well think I do) the difference between primitive types &
objects as parameters & thought because this would work or at the very least
the rowTitles array would as it's String's

Java parameter passing is pass-by-value. Reassigning the parameters
inside method does not affect their values outside the method. You'll
have to initialise your arrays outside loadData() and only do the
population in loadData().
Actually the references are passed by value, but the object they point
to is still accessible as if if was passed by reference. Therefore,
whilst you can't make the original reference point to a new object, you
can change the contents of the object, that the references point too.

I am fully aware of that. By "the value" I meant the reference value,
not the contents of the object that value refers to. In fact, if the
object wasn't reachable through the reference value, population of the
arrays in loadData() would be impossible, and I wouldn't recommend
something I knew didn't work, right?
 
A

Andrew McDonagh

Joona said:
Andrew McDonagh said:
Joona said:
harry <[email protected]> scribbled the following:

I have 2 multi-dim arrays

double[][] subTotals = null;
String[][] rowTitles = null;

I want to pass them to a function that initialises & populates them like
so -

loadData( rowTitles, subTotals);

this function contains lines like -

subTotals = new double[15][4];
rowTitles = new String[15][9];


They are then populated but when the funtion exits both arrays are back to
null.

I understand (well think I do) the difference between primitive types &
objects as parameters & thought because this would work or at the very least
the rowTitles array would as it's String's

Java parameter passing is pass-by-value. Reassigning the parameters
inside method does not affect their values outside the method. You'll
have to initialise your arrays outside loadData() and only do the
population in loadData().

Actually the references are passed by value, but the object they point
to is still accessible as if if was passed by reference. Therefore,
whilst you can't make the original reference point to a new object, you
can change the contents of the object, that the references point too.


I am fully aware of that. By "the value" I meant the reference value,
not the contents of the object that value refers to. In fact, if the
object wasn't reachable through the reference value, population of the
arrays in loadData() would be impossible, and I wouldn't recommend
something I knew didn't work, right?

Don't take it personally, I'm just trying to clarify for Harry's sake
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top