Results 1 to 6 of 6
  1. #1
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Is there a more simple neater way to call by reference for this code

    This is a "cody" represtntation of my java code that I figured out to
    effect call by reference since java does call by value and
    the calling program doesn't know the sizes of the arrays.
    Can someone show me a nicer way to accomplish this call by reference in java?
    Code in calling program:
    Java Code:
      double[][][]timSerMatWrp=new double[1][][]; //Wrp stands for wrap or carrier
      String[][]mainTitlsWrp=new String[1][]; 
      String[][]fctTitlsWrp=new String[1][]; 
      int[][]fctWidsWrp=new int[1][]; 
      int timLev=timLevOfTimAndFctSerRead(inFilStg,timSerMatWrp,mainTitlsWrp,fctTitlsWrp,fctWidsWrp); 
      double[][]timSerMat=timSerMatWrp[0]; //unwrap 
      String[]mainTitls=mainTitlsWrp[0]; 
      String[]fctTitls=fctTitlsWrp[0]; 
      int[]fctWids=fctWidsWrp[0]; 
      ................................
    Method called that does the work:
    Java Code:
    public final static int timLevOfTimAndFctSerRead(
     String inFilStg,
     double[][][]timSerMatWrp,
     String[][]mainTitlsWrp,
     String[][]fctTitlsWrp,
     int[][]fctWidsWrp) { 
        int nTitls0=3; //caller doesn't know this 
        int nTims0=500; //caller doesn't know this 
        int nFcts=5; //caller doesn't know this 
        int timLev=1; //caller doesn't know this 
        String[]mainTitls1=new String[nTitls0]; 
        double[][]timSerMat1=new double[nTims0][nFcts+1]; 
        String[]fctTitls1=new String[nFcts]; 
        int[]fctWids1=new int[nFcts]; 
        ..........
        timSerMatWrp[0]=timSerMat1; //transfers addresses to the wrappers 
        mainTitlsWrp[0]=mainTitls1; 
        fctTitlsWrp[0]=fctTitls1; 
        fctWidsWrp[0]=fctWids1; 
        return(timLev); 
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,735
    Rep Power
    7

    Default Re: Is there a more simple neater way to call by reference for this code

    First, why are you doing this?
    Java Code:
    String[][]fctTitlsWrp=new String[1][];
    //
    And since a 2D array is nothing but an array of 1D arrays you can do this:
    Java Code:
    String [][] foo = new String[10][8];
    String [] bar = foo[1];
    //
    But to answer your question, why not create a class of the values you want to pass around and change?
    Then just use a single reference of the class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Is there a more simple neater way to call by reference for this code

    I don't know enough java, so
    I don't understand you, but
    my guess is that the answer is
    "to prevent the 'spread-around' you mention".
    I like information local.
    I tried to pass the arrays with one fewer
    dimensions, but couldn't get the data arrays
    passed back with the correct sizes.
    I think you missed "calling program doesn't know the sizes".
    I also did the job that the subroutine does by
    using a class, but I don't like the complication.
    My subroutine seems simple, but
    I would like it more simple.
    Last edited by hill0093; 11-05-2013 at 07:14 PM.

  4. #4
    hill0093 is offline Member
    Join Date
    Mar 2013
    Posts
    20
    Rep Power
    0

    Default Re: Is there a more simple neater way to call by reference for this code

    I also noticed that if I add the following nulls, it still works.
    timSerMatWrp=null;
    mainTitlsWrp=null;
    fctTitlsWrp=null;
    fctWidsWrp=null;
    What is happening?
    Was an extra copy made so I could delete?

  5. #5
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    311
    Rep Power
    5

    Default Re: Is there a more simple neater way to call by reference for this code

    Man,
    your naming of the variables is so complicated I don't even try to understand your code, it would be much easier to get help if you simplify your code (make some simple SSCCE).

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Is there a more simple neater way to call by reference for this code

    Why don't you use a 'data transportation' object, where the callee fills in the member(s) of the objcect and the caller passes an empty data transportation object to the method?

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. call by reference or call by value in java?
    By proggrammer in forum New To Java
    Replies: 6
    Last Post: 06-27-2012, 03:00 PM
  2. Replies: 3
    Last Post: 06-22-2012, 06:54 PM
  3. How to call an XML-RPC method from Java Code
    By sreekanth.srk in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 12-31-2010, 01:36 PM
  4. Replies: 7
    Last Post: 09-24-2010, 05:09 AM
  5. call by value and call by reference in java
    By sandeepsai39 in forum New To Java
    Replies: 7
    Last Post: 08-12-2010, 11:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •