views:

233

answers:

5

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.

+1  A: 

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).

Tom Hawtin - tackline
A: 

Have you tried it? Arrays are already passed by reference in Java. When you call a function with a 1M element array it doesn't copy all 1M elements into a new array; it just passes a reference to the original one.

EDIT: I see now. 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.

Gabe
Yes, I have tried it, and I know that java doesn't create a whole array. Rather, it passes in a reference, or pointer, to the array. However, it doesn't actually pass in the reference used when the programmer calls the method, but rather creates a new one. So, when you would say 'arr = tmpArr', arr does begin to point to tmpArr, but when it leaves that method, arr, and whatever the programmer passed in for arr aren't the same, meaning that they get the old, unrevised, array.
Leif Andersen
+2  A: 

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)
polygenelubricants
Returning the new reference only works if you have a single array you're working with.
Gabe
You can return an array of arrays, or encapsulate multiple arrays as a new type.
polygenelubricants
The only problem with that would be that it would be awkward for the programmer to have to put the array in another array to call it, but I guess that would work for other, more appropriate situations.
Leif Andersen
+1  A: 

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;
       }
   }
Jim Ferrans
A: 

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.

Thiyaneshwaran S