Passing arrays as parameters problems...

Discussion in 'Java' started by harry, Jan 15, 2005.

  1. harry

    harry Guest

    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
     
    harry, Jan 15, 2005
    #1
    1. Advertisements

  2. 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 () ------------- 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
     
    Joona I Palaste, Jan 15, 2005
    #2
    1. Advertisements

  3. harry

    au714 Guest

    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
     
    au714, Jan 15, 2005
    #3
  4. 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;
    }
     
    Andrew McDonagh, Jan 15, 2005
    #4
  5. 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 I Palaste, Jan 15, 2005
    #5
  6. Don't take it personally, I'm just trying to clarify for Harry's sake
     
    Andrew McDonagh, Jan 15, 2005
    #6
  7. Ah, OK.
     
    Joona I Palaste, Jan 15, 2005
    #7
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.