0

I have an Array:

int[] arr = {-15, -23, -34, -22, 2};

I am trying to do an if statement, if values are below 0, then the array value changes to -1. If the values are above 0 the array values are changed to 1.

     public static void main(String[] args) {
        int[] swap;
        int[] arr = {-15, -23, -34, -22, 2};
        swap = arr;
        
        
         for (int x = 0; x < arr.length; x++) {
             System.out.println(arr[x]+swap[x]);
         }
        for (int i = 0; i < arr.length; i++) {
            // System.out.println(arr[i]);
            if (arr[i] < 0) {
                arr[i] = -1;
                System.out.println("Oringinal value "+swap[i] + " Error Code: " + arr[i]);
            }
            if (arr[i] > 0) {
                arr[i] = 1;
                System.out.println("Oringinal value "+swap[i] + " Error Code: " + arr[i]);
            }
        }
    }

My current code is above.

The objective is to print the original array value with the changed "error code".

However the print out is retuning the same value with the original value.

Oringinal value -1 Error Code: -1
Oringinal value -1 Error Code: -1
Oringinal value -1 Error Code: -1
Oringinal value -1 Error Code: -1
Oringinal value 1 Error Code: 1

What am I doing wrong?

5
  • 1
    Please show the declaration of the swap-Array. Commented Jan 2, 2021 at 23:34
  • 2
    int[] swap = Arrays.copyOf(arr, arr.length); Commented Jan 2, 2021 at 23:35
  • We don't know what swap is, but you see the new value of arr[i] simply because you set it prior to printing it. Commented Jan 2, 2021 at 23:36
  • changed the code above Commented Jan 2, 2021 at 23:39
  • Use @IłyaBursov's suggestion, or make a copy in the loop itself. swap=arr does not make a copy of the elements of the array, this way arr and swap are two names for a single array. Commented Jan 2, 2021 at 23:40

4 Answers 4

4
int[] swap;
int[] arr = {-15, -23, -34, -22, 2};
swap = arr;

With the last command, you are not copying the array, but assigning a reference to the same array in arr to the swap variable. As a result, any change on arr will be reflected on swap.

In order to clone the array and persist the original values, you could initialize it like this, creating a deep-copy:

int[] swap = Arrays.copyOf(arr, arr.length);

As noted by Arvind, you can also call arr.clone() on arr to create a shallow copy:

int[] swap = arr.clone();

Shallow copies on primitive arrays don't have the side effects that Objects (excluding Strings) do here.

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

Comments

1

int[] arr = {-15, -23, -34, -22, 2};

arr is a reference. It currently points at that array; consider the array treasure, and arr the map that can find it.

int[] swap = arr;

This makes a new treasuremap, which is pointing at the exact same treasure.

arr[i] = 1;

This follows the arr map to find the X, digs down, opens the treasure chest, and does a bunch of stuff there. swap, given that it's a different map but one that leads to the exact same treasure, now has the property that if you follow that one, you'll find the same changes. There is, after all, only one treasure.

The solution is to replicate that treasure. Arrays.copyOf(arr) can do it, for example.

Comments

1

The swap must be a deep copy of arr, such as

int[] arr = {-15, -23, -34, -22, 2};
int[]swap = Arrays.copyOf(arr, arr.length);

Comments

1
int[] swap;
int[] arr = {-15, -23, -34, -22, 2};
swap = arr;

With this code, you actually saving the reference of the arr array in swap. Which means both names are referencing the same array. So you have to copy the array called arr.

int[]swap = Arrays.copyOf(arr, arr.length);

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.