代码之家  ›  专栏  ›  技术社区  ›  Maciej Gryka

如何有效地从Android资源中获取短阵列?

  •  5
  • Maciej Gryka  · 技术社区  · 17 年前

    我有一个XML文件,在我的资源中存储了一组短裤。我需要分配这个数组,但是android api只提供了一种获取ints数组的方法( 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];
    }
    
    1 回复  |  直到 10 年前
        1
  •  2
  •   sooniln    10 年前

    无法直接检索短裤数组afaik。如果你能用一个 int[] 代替你的 short[] . 如果您使用的是一个库或者其他什么东西,并且不能改变需求,那么您的示例几乎是最好的方法。小心可能溢出。

    推荐文章