PVector to array
in
Core Library Questions
•
2 years ago
About 18 months ago I was writing some software to load and animate Quake 2 models so that they could be used in Processing.
The 3D model obviously had a large number of 3D coordinates and it seemed obvious to use the PVector class. When I looked at the source code I found the expected attributes for x, y and z but also found one called array. Eventually I found the one and only method that used this attribute - array() that created and returned the PVector as an array. Even if the method was never called it increased the required memory for each PVector by a third. It is marked as transient so at least the array won't be serialised but I wonder why not code the method as
public float[] array() {
float[] array = new float[3];
array[0] = x;
array[1] = y;
array[2] = z;
return array;
}
so removing the need for the extra attribute.
If the method was to be used extensively I could see the benefit from not creating all those arrays, but then the documentation recommends to use the get method to populate an existing array in preference to this method.
I am just curious about the rationale behind having the extra attribute after all the toArray method in Java creates a new array when called.
The 3D model obviously had a large number of 3D coordinates and it seemed obvious to use the PVector class. When I looked at the source code I found the expected attributes for x, y and z but also found one called array. Eventually I found the one and only method that used this attribute - array() that created and returned the PVector as an array. Even if the method was never called it increased the required memory for each PVector by a third. It is marked as transient so at least the array won't be serialised but I wonder why not code the method as
public float[] array() {
float[] array = new float[3];
array[0] = x;
array[1] = y;
array[2] = z;
return array;
}
so removing the need for the extra attribute.
If the method was to be used extensively I could see the benefit from not creating all those arrays, but then the documentation recommends to use the get method to populate an existing array in preference to this method.
I am just curious about the rationale behind having the extra attribute after all the toArray method in Java creates a new array when called.
1