I came up to a situation where I have an array and I need to copy some specific attributes (i.e. values at specific indinces) not the whole array to another array.
For example if the initial array is:
double[] initArray = {1.0, 2.0, 1.5, 5.0, 4.5};
then if I wanted to copy only 2nd, 4th and 5th attribute (i.e. values at these indices) the desired output array would be:
double[] reducedArray = {2.0, 5.0, 4.5};
I know that if the indices appear in a sequential form, e.g. 1-3 then I can use System.arraycopy()
but my indices does not have that aspect.
So, is there any official way to do this, besides the trivial loop through each value and copy the ones needed:
double[] includedAttributes = {1, 4, 5};
double[] reducedArray = new double[includedAttributes.length];
for(int j = 0; j < includedAttributes.length; j++) {
reducedArray[j] = initArray[includedAttributes[j]];
}