【发布时间】: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