Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently get short array from resources on Android?

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];
}
like image 408
Maciej Gryka Avatar asked Apr 10 '09 19:04

Maciej Gryka


1 Answers

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 your short[]. 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.

like image 165
sooniln Avatar answered Nov 15 '22 00:11

sooniln