如何有效地从Android资源中获取短数组?

Mac*_*yka 6 java arrays resources android

我有一个XML文件,在我的资源中存储了一系列短路.我需要分配这个数组,但Android API只提供了一种获取int数组的方法getResources.getIntArray().我最终获得了一系列的整数,然后将它转换为短裤阵列,但这并不理想.

有没有办法从Android资源中获取一系列短裤?如果没有,是否有更有效/更清洁的方法将int数组转换为短数组?我的代码目前看起来像这样:

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];
}
Run Code Online (Sandbox Code Playgroud)

soo*_*iln 2

据我所知,没有办法直接检索短裤数组。看看您是否可以使用 anint[]代替您的short[]. 如果您正在使用库或其他东西并且无法更改需求,那么您的示例几乎是最好的方法。警惕可能的溢出。