I am having a problem where, after performing a simple mathematical operation on the elements of an array, I return this array and receive only what was changed in the specific function.
You may be thinking, "Great. That's what's supposed to happen." You are correct, but let me give another situation: I call functionA
below, then I call functionB
. Instead of getting 1 1 1
as the output, I get 0 3 -2
, the exact same as functionB
without the call to functionA
.
Here's the example's code:
public static int[] position = new int[3]; //This array is filled
//with [0,0,0] using a for-loop
public static int[] functionA(int[] position){
position[0] =+ 1; //=+ operator use
position[1] =- 2; //=- operator use
position[2] =+ 3;
return position;
}
public static int[] functionB(int[] position){
position[0] =+ 0;
position[1] =+ 3;
position[2] =- 2;
return position;
}
functionA(position);
functionB(position);
System.out.println(myPosition[0]); //0
System.out.println(myPosition[1]); //1
System.out.println(myPosition[2]); //-2
I do not understand where I went wrong. Am I using the =+ or =- operators incorrectly or am I returning the array incorrectly, or is there something I am totally missing?
Any help is appreciated.