Results 1 to 16 of 16
  1. #1
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default Sending array index by reference

    Hi there:

    Could anyone please tell me how to send an array index to a method by reference? I want to do something like this:

    void foo1()
    {
    int[] arr = { 1,2,3,4,5 };
    foo2( arr + 1 ); //It gives me a compile error here
    }

    void foo2( int[] arr )
    {
    arr[0] ++;
    }

    In C++ I'd simply call foo2 from foo1 like this:
    foo2( arr + 1 );
    OR
    foo2( &arr[ 1 ] );

    Any help would be appreciated.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    An array index is an int which is a primitive, primitives don't have references, and Java is pass-by-value and pass-by-value only. If you want to change what an array contains in a specific index than either have the method return the new object and assign the element to the array from outside the method, or pass the array reference value and the index as parameters to the method.

  3. #3
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Hi:

    But I'm not sending an int over to foo2, I'm sending an array of integers to foo2. I only want to change the start index of the array, i.e. I want to send the array starting from index 1.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then pass the array and an index (to start from).

    Edit: So, what part of
    Quote Originally Posted by masijade View Post
    If you want to change what an array contains in a specific index than either ..., or pass the array reference value and the index as parameters to the method.

  5. #5
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    There are limitations in my program and I'm not able to change the argument list...

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then you are going to have to change the array that you're passing. copy it, shifting the positions, and copy it back when you're done. Or rethink what it is you're doing and how you're doing it.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    What exactly are you trying to do that you feel the need to do it this way.

  8. #8
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks for the suggestions. However, I'm not able to copy a whole array just because of this and am looking for a better solution. I have a recursive method that will pass an array that starts from the next index every time. I could do that very simply in C++, so I'm assuming there's got to be a way to do it in Java too. After all, an array name is just a pointer pointing to the first index, and all I want is to point it to the next index...

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That's why you need to tell it what index to point to by passing that index as an argument. If you are unwilling (or,as you claim unable) to do that it is not my fault, nor the fault of the language.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mattie View Post
    void foo1()
    {
    int[] arr = { 1,2,3,4,5 };
    foo2( arr + 1 ); //It gives me a compile error here
    }

    void foo2( int[] arr )
    {
    arr[0] ++;
    }
    C and C++ have pointer arithmetic; a lovely little feature that ruins static type safety checks and causes a lot of trouble in the hands of the inexperienced; Java doesn't have that. You have to change your method foo2( ... ) to:

    Java Code:
    void foo2(int[] arr, int i) {
       arr[i]++;
    }
    ... or do as Masijade suggested: copy part of the array over to another one and use that (a rather expensive hack). Changing code from C or C++ when it uses pointer arithmetic can be a burden ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Already suggested also, claims he can't change the method declaration.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by masijade View Post
    Already suggested also, claims he can't change the method declaration.
    Ah, I missed that; sorry about that; well, the OP is out of luck then (or he has to implement that silly array copy hack ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I have no problems with it. Feel free to hack away at me! ;-)

  14. #14
    mattie is offline Member
    Join Date
    Jan 2011
    Posts
    9
    Rep Power
    0

    Default

    Thanks guys. I'm a "she" by the way, lol!
    This sucks then... What is the object type of an array? Could I somehow use type casting, or operator +, etc.?

  15. #15
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mattie View Post
    This sucks then... What is the object type of an array? Could I somehow use type casting, or operator +, etc.?
    It doesn't suck; you're trying to transform a piece of code from one language to another; one language has pointer arithmetic, the other one doesn't. There's no way you can trick it into it with obscure use of operators ... It doesn't have pointer arithmetic.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    How are you going to do recursion with a void method anyway?

    -Gary-

Similar Threads

  1. get index from an array (very frustrating)
    By Yakg in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 06:26 PM
  2. Using variable as array index
    By SalParadise in forum New To Java
    Replies: 9
    Last Post: 11-16-2010, 09:56 PM
  3. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  4. Array Index problems
    By ragnor2004 in forum New To Java
    Replies: 4
    Last Post: 03-26-2009, 07:53 PM
  5. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 11:56 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
  •