I've got an XML file storing an array of shorts in my resources. I need to assign this array, but Android API only provides a way to get array of ints (getResources.getIntArray()
). I've ended up getting an array of ints and converting it to array of shorts later, but that's hardly ideal.
Is there a way to get an array of shorts from Android resources? If not, is there a more efficient/cleaner way of converting int array into short array? My code currently looks like this:
int indicesInt[] = context.getResources().getIntArray(R.array.indices);
short indices[] = new short[indicesInt.length];
for (int i = 0; i < indicesInt.length; i++) {
indices[i] = (short)indicesInt[i];
}
Best Solution
There is no way to directly retrieve an array of shorts AFAIK. It would be worth it to see if you can just use an
int[]
in place of yourshort[]
. If you're using a library or something and can't change the requirement, your example is pretty much the best way to do it. Be wary of possible overflow.