Arrays are passed by-reference, not by-value. That means in your function, where you are sorting array a, you are sorting p1 and returning it. When you assign that returned array to p2, you are wiping out the previously created array and causing the variable p2 to also point to the same array as p1.
Instead, in your bsort function, you could create a new array b, copy the values from a into b, then sort and return array b, and that might give you what you expected.
Think of computer memory as a city full of houses where you can store something in each house. An array variable, such as int a is an address to a row of houses. When you say a = new int;, you are asking the computer to find you five empty houses and give you their address. int b is another address. If you say b = a;, then both a and b have the same address to those same five houses. They don’t have a copy of the houses, they are the same five houses. If I say a = 17; then b will also be 17.
When you pass p1 into your bsort() function, you are passing the address of the array to the function. So any changes you make to elements of that array inside the function where it is called a are changing the same houses as those pointed to by p1. arrayCopy() is shorthand for creating a new array and then looping through the elements in the old array and copying them to the new array.
Even then, you still have to be careful when you have an array of objects. In your case, you have an array of PVectors. PVectors are objects. Objects are also really addresses to yet a different house somewhere else in memory. Even after you copy your array, if you modify, for instance, just the y component of one of your vectors, you’ll see the same change in both arrays. But if you replace one of the elements with a new PVector in one array, they will now point to two different PVectors and so can have separate values. If you were to create entirely new PVectors in your second array and fill them with the values of the vectors in the first, this is what’s called a “deep copy”. In your case, you do not need a deep copy, but you do want the shallow copy so that you can sort the second array without rearranging the first one.
That’s a great explanation, thanks. I have recently encountered the fact that i couldn’t manipulate array layers in c# as easily as in Processing, and i assume, the way Processing approaches arrays and the interacions between them allows for doing a lot of stuff much better performance-wise, while having drawbacks, such as the behaviour similar to what caused the problem i faced. Before i faced it, i thought, that by saying
int a = new int ;
int b = a;
i was creating two separate arrays that are just equal. It seems strange though that i didn’t bump into this earlier, but it might explain many problems i left unsolved over the last couple years of occasionally fooling around in Processing. Thanks again!
Although that’s a valid PoV, officially Java passes everything by value.
For object datatypes such as arrays or strings, those values are memory address, a.K.a. reference or pointer.
Object datatypes are everything else which isn’t 1 of the 8 primitive datatypes:
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
By that definition, all languages only pass by value. The point is, if the value being passed is the memory address of the data being referenced, we call it passing by-reference.
C/C++ and Java are no different in that regard. C/C++ just happens to have the additional mechanism where objects (or structs) can exist on the stack such that direct memory copies of them can be passed by value.
Arguably, “object reference” should be the 9th primitive datatype.