By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,727 Members | 768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,727 IT Pros & Developers. It's quick & easy.

Passing arrays as parameters problems...

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
harry <sp***********@yahoo.co.uk> 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().

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- 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
Jul 17 '05 #2

P: n/a
Joona I Palaste wrote:
harry <sp***********@yahoo.co.uk> 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.

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;
}
Jul 17 '05 #3

P: n/a
Andrew McDonagh <ne**@andrewcdonagh.f2s.com> scribbled the following:
Joona I Palaste wrote:
harry <sp***********@yahoo.co.uk> 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?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A bicycle cannot stand up by itself because it's two-tyred."
- Sky Text
Jul 17 '05 #4

P: n/a
Joona I Palaste wrote:
Andrew McDonagh <ne**@andrewcdonagh.f2s.com> scribbled the following:
Joona I Palaste wrote:
harry <sp***********@yahoo.co.uk> 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
Jul 17 '05 #5

P: n/a
Andrew McDonagh <ne**@andrewcdonagh.f2s.com> scribbled the following:
Joona I Palaste wrote:
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


Ah, OK.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.