【问题标题】:Trouble using indexOf with list of drawable resources将 indexOf 与可绘制资源列表一起使用时遇到问题
【发布时间】:2014-12-01 00:13:10
【问题描述】:

将 indexOf 与可绘制资源列表一起使用时遇到问题。我的索引 i 给我的值为 -1(它没有找到可绘制资源)。

Booster booster = new Booster();
Booster booster1 = booster.findBooster(R.drawable.booster_empty);

public Booster findBooster(int boosterID) {

    int[] boosterIDs = new int [] {
            R.drawable.booster_empty,
            R.drawable.booster_posts_1,
    };

    String[] names = new String[] {
            "blah",
            "blah"
    };

    String[] descriptions = new String[] {
            "blah",
            "blah"
    };

    String[] boosts = new String[] {
            "blah",
            "blah"
    };

    int i = Arrays.asList(boosterIDs).indexOf(boosterID);

    Booster booster = new Booster(boosterID, names[i], descriptions[i], boosts[i]);

    return booster;
}

我知道这可能是我缺少的一些非常简单的东西。请帮忙!

【问题讨论】:

    标签: java android list drawable indexof


    【解决方案1】:

    在原始数组上调用 Arrays.asList() 将产生一个包含一个元素(即数组)的列表,而不是您预期的数组到列表的转换。尝试将int[] 更改为Integer[]

    【讨论】:

      【解决方案2】:

      List.indexOf() 需要一个对象,但你传递了一个 int,这就是为什么 int 是原始数据类型,而不是对象,这就是它找不到你想要的东西的原因

      【讨论】:

      • int 自动装箱为 Integer
      • 正确,但是假设您传入 1,它会自动装箱为 Integer 对象 1,但是 indexOf 方法需要对象的名称,而不是对象的索引,明白吗?
      • 不。 OP 正在尝试创建一个 List<Integer>List<int> 真的,但没有这样的东西),但最终创建了一个 List<int[]> (参见贾斯汀的回答)。当通过int 时,从List<Integer> 搜索将按照OP 的预期工作,因为它已通过equals() 进行检查,但不幸的是创建的列表类型错误。
      • 是的,但是 indexOf 方法仍然需要对象的名称,但是他传入了 boosterId,而不是 int。
      • ...自动装箱到Integer,并通过equals() 对列表中的项目进行检查。如果是List<Integer>,则会找到相应的元素。忘记“名称”,它们在运行时不存在。
      【解决方案3】:

      Arrays.asList(...) 方法接收所有数组元素并返回包含这些每个元素的 List 实例。在您的情况下,您添加了 1 个数组(对象),它是一个元素。因此,当您访问任何大于 0 的元素时,它会抛出 ArrayIndexOutOfBoundsException。请参阅下面的带有输出的代码示例。

              //Changed the code to illustrate the best without depending on the project.
              int[] boosterIDs = new int[] { 1, 2 };
              //Arrays.asList() return an List object which contains only 1 element of type int[]
              List<int[]> asList = Arrays.asList(boosterIDs);
              for (int j = 0; j < asList.size(); j++) {
                  int[] item = asList.get(j);
                  System.out.println("Loop = " + j + "length = " + item.length + " Contents...");
                  for (int k = 0; k < item.length; k++) {
                      System.out.print(item[k] + " ");
                  }
                  System.out.println();
              }
      
              System.out.println("Printing second loop");
              int[] secondBooterIds = new int[] { 3, 4 };
              //Arrays.asList() return an List object which contains only 2 elements of type int[]
              List<int[]> secondList = Arrays.asList(boosterIDs, secondBooterIds);
              for (int j = 0; j < secondList.size(); j++) {
                  int[] item = secondList.get(j);
                  System.out.println("Loop = " + j + "length = " + item.length + " Contents...");
                  for (int k = 0; k < item.length; k++) {
                      System.out.print(item[k] + " ");
                  }
                  System.out.println();
              }
      

      输出

      Loop = 0length = 2 Contents...
      1 2 
      Printing second loop
      Loop = 0length = 2 Contents...
      1 2 
      Loop = 1length = 2 Contents...
      3 4 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-20
        • 2021-07-15
        • 2013-06-19
        • 2014-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多