2

I wrote this test,

public class test {     
    public static String[] foo(String[] myArray, int index) {
        myArray[index] = "world";
        return myArray;
    }

    public static void main(String[] args) {
        String[] fooArray = new String[10];

        for (int i = 0; i < 10; i ++) {
            fooArray[i] = "hello";
        }

        foo(fooArray, 9);

        for (int i = 0; i < 10; i ++) {
            System.out.println(fooArray[i]);
        }
    } 
}

which results in this output

hello
hello
hello
hello
hello
hello
hello
hello
hello
world

So foo basically just changes a value in the array and returns this modified array. But the actual fooArray is being modified by the foo method (as evidenced by the output). Is there an efficient way to pass the array fooArray and have it not modified by foo?

I know that I could make a copy of the fooArray in the method like

    public String[] void foo(String[] myArray, int index) {
        String[] copy = new String[];

        for (int i = 0; i < 10; i ++) {
            copy[i] = myArray[i];
        }

        copy[index] = "world";
        return copy; 
    }

but I am planning to do this modification on arrays of a size in the thousands and also I am planning to this millions (or even billions) of times during runtime so I don't think that copying the entire array every time would be an efficient solution (I need to get a reasonable runtime).

takendarkk
  • 3,347
  • 8
  • 25
  • 37
Lucas Alanis
  • 1,208
  • 3
  • 15
  • 30
  • check this one first ... http://stackoverflow.com/questions/14491405/javascript-passing-arrays-to-functions-by-value-leaving-original-array-unaltere – Tun Zarni Kyaw Feb 17 '15 at 04:17
  • So you make a modification but don't assign it to anything. What is the point of the `foo()` method anyway? Are you aware that Java is pass-by-value? – takendarkk Feb 17 '15 at 04:21
  • 1
    Welcome to the magic world of references! You have to effectively copy the array. – fps Feb 17 '15 at 04:26

2 Answers2

4

I think the best solution is Arrays.copyOf(...)

It uses System.arraycopy(...) for example in Arrays.java you can find:

public static byte[] copyOf(byte[] original, int newLength) {
    byte[] copy = new byte[newLength];
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

And the documentation says it is efficient because it is native method and just copies the old block of memory to new one.

Vitaly
  • 2,552
  • 2
  • 18
  • 21
0

Simply do fooArray.clone() For example:

foo(fooArray.clone(), 9);

This will clone the array and when you call foo it will not modify fooArray.

Forseth11
  • 13
  • 6
  • 1
    Using `clone` is a bad habit. Better use `System. arraycopy()`: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#arraycopy(java.lang.Object,%20int,%20java.lang.Object,%20int,%20int) or `Arrays.copyOf(...)` as Vitaly has mentioned. – Nir Alfasi Feb 17 '15 at 04:27
  • read this: http://howtodoinjava.com/2012/11/10/cloneable-interface-is-broken-in-java/ – Nir Alfasi Feb 17 '15 at 05:51