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

3
  • check this one first ... stackoverflow.com/questions/14491405/… Commented Feb 17, 2015 at 4: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? Commented Feb 17, 2015 at 4:21
  • 1
    Welcome to the magic world of references! You have to effectively copy the array. Commented Feb 17, 2015 at 4:26

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Using clone is a bad habit. Better use System. arraycopy(): docs.oracle.com/javase/7/docs/api/java/lang/… or Arrays.copyOf(...) as Vitaly has mentioned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.