【问题标题】:Android : Can't get the index 1 on ArrayListAndroid:无法获取 ArrayList 上的索引 1
【发布时间】:2017-07-29 04:43:09
【问题描述】:

我知道这个问题对于这里的一些成员来说听起来很愚蠢,但我已经不知道了。

我遇到了一个奇怪的数组问题。我尝试在ArrayList 上获取index 1,但它总是抛出java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

我已通过以下代码进行调试:

for (int o=0; o<vcard.size(); o++){
    if(vcard.get(o).contains(":")){
        System.out.println("Contains");
        String[] splitByTitikDuaVCard = vcard.get(o).split(Pattern.quote(":"));
        //this line show the length is 2 
        System.out.println("RESULT LENGTH : " + splitByTitikDuaVCard.length );
        for (int y=0; y<splitByTitikDuaVCard.length; y++){
            System.out.println("RESULT Y : " + splitByTitikDuaVCard[y] + ", INDEX : " + y);
            mapContentTerbaruVCard.put(splitByTitikDuaVCard[0], splitByTitikDuaVCard[1]);
        }
    }else{
        System.out.println("Not Contains");
    }
}

log 的结果:

03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : BEGIN, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : VCARD, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : VERSION, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : 3.0, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : N, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : Doe;John, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : FN, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : John Doe, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ORG, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : PT. Pertamina, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TITLE, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : Bussiness Manager, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ADR, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : ;;Soekarno Hatta;Bandung;;46153;West Jave, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;WORK;VOICE, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : 021 203312, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 2
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;CELL, INDEX : 0
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : +62818456463111, INDEX : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 1
03-08 23:00:18.594 8350-8350/? I/System.out: RESULT Y : TEL;FAX, INDEX : 0

log 表明存在index 1,但是当我尝试将index 1 放入HashMap 时,出现错误java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

我的问题是:如何找到index 1,以便我可以将其保存到hashmap

注意:我已经尝试过这个 (Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1) 解决方案,但不适用于我的情况。

【问题讨论】:

  • 错误告诉你ArrayList的大小为1,因此唯一元素的索引为0。
  • "日志显示索引 1 存在" 请显示此日志。此外,您应该给出一个完整的代码示例。请务必将您的代码放入类中的方法中。最后,发布整个堆栈跟踪并向我们展示导致错误的行。
  • 数组从 0 开始计数。基本上错误消息告诉您的内容如下:您有一个包含 1 个元素的数组/数组列表,并且您尝试访问索引 1 处的元素,这实际上是第二个元素列表中的元素。 (或者不是,因为你有一个错误)。
  • 说“我试过这个......”是不够的。您的代码永远不会与链接中给出的代码完全相同。这意味着您需要确切地向我们展示您尝试了什么,以便我们可以从那里帮助您。
  • 见我上面的第二条评论。

标签: java android arraylist indexoutofboundsexception


【解决方案1】:

splitByTitikDuaVCard 可能有lenght = 1,所以[1] 的位置超出了它的范围。

对于 OP,请自行尝试。你会注意到长度是 1。

public class Test
{
   public static void main(final String[] args) {
      final String[] splitted = "String:".split(":");
      System.out.println(splitted.length);
   }
}

查看您的日志:

03-08 23:00:18.594 8350-8350/? I/System.out: RESULT LENGTH : 1

长度 = 1

【讨论】:

  • 但我已将其拆分为 :,因此它应该包含 2 个索引,因此 index 0index 1,请纠正我。
  • @Koma 查看更新的答案,它将解释可能发生的情况。
  • 不,先生。我做String[] splitByTitikDuaVCard = vcard.get(o).split(Pattern.quote(":"));System.out.println("RESULT LENGTH : " + splitByTitikDuaVCard.length ); 长度显示2
  • 不可能。发布堆栈跟踪。
  • 查看更新的答案。您日志的最后一条记录显示长度 = 1。所以我的猜测是正确的。
【解决方案2】:

在第二个循环(内循环)中,您从 splitByTitikDuaVCard[1] 获得价值,为什么您将索引值硬编码。你应该使用 y 的值。

替换这个

mapContentTerbaruVCard.put(splitByTitikDuaVCard[0], splitByTitikDuaVCard[1]);

有了这个

mapContentTerbaruVCard.put("key"+y, splitByTitikDuaVCard[y]);

在此代码中,您的密钥将是 key0, key1, ....

试试这个代码,它会帮助你

【讨论】:

  • “键”到底是什么意思?我的情况我需要将splitByTitikDuaVCardindex 0 保存为keyhasmap
  • keyHashMap 中值的标识。你不知道HashMap 中的value,但你知道key,那么你可以很容易地找到特定key 的值。是的,将索引存储为键,这样您就可以从索引中获取价值。但不要存储 硬编码 值。再次查看我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-28
  • 1970-01-01
相关资源
最近更新 更多