简体   繁体   中英

Changing values of an array in Java

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?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM