473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing arrays as parameters problems...

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
5 12575
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: PerfectDayToChaseTornados | last post by:
Hi All, I am trying to create a BeanHelper class which will set a beans values from the request much the same as it would be form a jsp using 'usebean'. I can get everything to work except for...
39
by: Mike MacSween | last post by:
Just spent a happy 10 mins trying to understand a function I wrote sometime ago. Then remembered that arguments are passed by reference, by default. Does the fact that this slowed me down...
4
by: David Thorp | last post by:
New to this list (first post), and relatively new to C, so hi everyone... If anyone can help me with this I'll be most grateful... The following code is from a rather elaborate (for me) program...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
2
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
3
by: ZMan | last post by:
The following code won't compile with gcc version 3.4.2 (mingw-special). How come? Error: cannot convert `char (*)' to `char**' /**********************************************************/...
4
by: Nathan Sokalski | last post by:
I am a beginner with AJAX, and have managed to learn how to use it when passing single parameters, but I want to return more than one value to the client-side JavaScript function that displays it....
2
by: luis | last post by:
I'm using ctypes to call a fortran dll from python. I have no problems passing integer and double arryas, but I have an error with str arrys. For example: ..... StringVector = c_char_p *...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.