Leif Andersen
Leif Andersen

Reputation: 22332

Simulating pass by reference for an array reference (i.e. a reference to a reference) in Java

I was wondering, in java, is it possible to in anyway, simulate pass by reference for an array? Yes, I know the language doesn't support it, but is there anyway I can do it. Say, for example, I want to create a method that reverses the order of all the elements in an array. (I know that this code snippet isn't the best example, as there is a better algorithms to do this, but this is a good example of the type of thing I want to do for more complex problems).

Currently, I need to make a class like this:

public static void reverse(Object[] arr) {
    Object[] tmpArr = new Object[arr.length];
    count = arr.length - 1;
    for(Object i : arr)
        tmpArr[count--] = i;
    // I would like to do arr = tmpArr, but that will only make the shallow
    // reference tmpArr, I would like to actually change the pointer they passed in
    // Not just the values in the array, so I have to do this:
    for(Object i : tmpArr)
        arr[count++] = i;
    return;
}

Yes, I know that I could just swap the values until I get to the middle, and it would be much more efficient, but for other, more complex purposes, is there anyway that I can manipulate the actual pointer?

Again, thank you.

Upvotes: 3

Views: 958

Answers (5)

Gabe
Gabe

Reputation: 86718

You want to pass a reference to the array reference. In that case you just have to either create a class to hold the reference and pass a reference to that class or just pass a 1-element array of the type being passed. Then you'd be passing either an object holding the array or an array whose only element contains the array you want to operate on.

Upvotes: -1

Jim Ferrans
Jim Ferrans

Reputation: 31012

This method reverses the Array's elements in place. The caller sees the changes. (In Java everything is passed by value, including object references.)

   public static void reverse(Object[] arr) {
       for ( int i = 0, j = arr.length - 1;   i < j;   i++, j-- ) {
           Object temp = arr[i];
           arr[i] = arr[j];
           arr[j] = temp;
       }
   }

Upvotes: 1

Thiyaneshwaran S
Thiyaneshwaran S

Reputation: 1135

In Java Object reference is passed by value.

So if you looking for something like

function referenceCheck()
{
    int[] array = new int[]{10, 20, 30};
    reassignArray(&array);
    //Now array should contain 1,2,3,4,5
}

function reassignArray(int **array)
{
    int *array = new int[] { 1, 2, 3, 4, 5};
}

Then its not possible in Java by any direct means.

If we need to change only the values stored in an array, then we can do it since object reference is passed by value.

Upvotes: 0

polygenelubricants
polygenelubricants

Reputation: 383746

is there anyway that I can manipulate the actual pointer?

Java does not pass by reference, so you can't directly manipulate the original pointer. As you've found out, Java passes everything by value. You can't pass a reference to an array object, and expect a method to modify the original reference to point to another array object.

You can, of course:

  • Modify elements of the referred array object (ala java.util.Arrays.sort)
  • Pass a reference to an object with a settable field (e.g. Throwable has a setStackTrace)
  • return the new reference instead (ala java.util.Arrays.copyOf)

Upvotes: 2

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Well, you can explicitly pass an object that contains a reference. java.util.concurrent.atomic.AtomicReference is ready out of the box, although it does come with volatile semantics that you probably don't want. Some people use single element arrays to returns values from anonymous inner classes (although that doesn't seem a great idea to me).

Upvotes: 1

Related Questions