1

Taking into consideration that Java operates off of pass-by-value. Which of the following would be deemed the most conventional/correct way to assign a new array value to an already existing array:

1:

 int[] x = {1,2};

    public static int[] changeValue(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i]*=2;
        }
        return arr;
    }

    changeValue(x);

2:

 int[] x = {1,2};

    public static int[] changeValue(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i]*=2;
        }
        return arr;
    }

    x = changeValue(x);

3:

 int[] x = {1,2};

    public static void changeValue(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i]*=2;
        }
    }

    changeValue(x);

Thanks.

5
  • 1
    The third one works. You're passing a copy of the reference but what you're modifying is the contents of the actual object. Commented Mar 26, 2019 at 16:53
  • They all work, but I would have thought that just conventionally the second one would of made the most sense? Commented Mar 26, 2019 at 16:55
  • @Danny have you considered the Stream option? Commented Mar 27, 2019 at 16:09
  • @LppEdd The stream option does seem to be rather good, however I was more interested in figuring out which of the 3 options I listed was the most conventional. Commented Mar 27, 2019 at 16:17
  • @Danny imho the third one. A void return type explicitly says that the input array will be modified. Returning int[] might be misleading. A user might think a new array is returned, while it is not. But still, prefer immutability. Commented Mar 27, 2019 at 16:19

3 Answers 3

2

Either is fine. As for code guidelines you can stick to the native java methods e.g. Collections#reverse does not return anything. It mutates the passed object. So it really depends on exactly what the method does.

So in your case I would go with the third version.

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

Comments

2

Actually I would prefer return a new array instead of modified existing one:

public static int[] changeValue(int[] arr) {
    int[] newArr = new int[arr.length];
    for (int i = 0; i < arr.length; i++) {
        newArr[i] = arr[i] * 2;
    }
    return newArr;
}

See more about immutability importance

And Why is immutability so important in software development

1 Comment

Or perhaps keeping the same method, but adding arr = Arrays.copyOf(arr, arr.length); instead of declaring a new variable.
0

Using a Stream is the cleanest way

public static int[] changeValue(final int[] arr) {
   return IntStream.of(arr)
                   .map(i -> i *= 2)
                   .toArray();
}

This will also let you pass in a customized/variable transformation function

public static int[] changeValue(final int[] arr, final IntUnaryOperator function) {
    return IntStream.of(arr)
                    .map(function)
                    .toArray();
}

final int[] result1 = changeValue(ints, i -> i *= 2);
final int[] result2 = changeValue(ints, i -> i /= 4);

Immutability, clean code, and parallelizable (.parallel()) for big arrays.

Comments

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.