Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 安卓:Can';无法在ArrayList上获取索引1_Java_Android_Arraylist_Indexoutofboundsexception - Fatal编程技术网

Java 安卓:Can';无法在ArrayList上获取索引1

Java 安卓:Can';无法在ArrayList上获取索引1,java,android,arraylist,indexoutofboundsexception,Java,Android,Arraylist,Indexoutofboundsexception,我知道这个问题对这里的一些议员来说听起来很愚蠢,但我已经不知道了 我在数组方面遇到了一个奇怪的问题。我试图在ArrayList上获取索引1,但它总是抛出java.lang.ArrayIndexOutOfBoundsException:length=1;索引=1 我已通过以下代码进行调试: for (int o=0; o<vcard.size(); o++){ if(vcard.get(o).contains(":")){ System.out.println("Co

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

我在数组方面遇到了一个奇怪的问题。我试图在
ArrayList
上获取
索引1
,但它总是抛出
java.lang.ArrayIndexOutOfBoundsException:length=1;索引=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");
    }
}
日志
显示
索引1
存在,但当我尝试将
索引1
放入
HashMap
时,错误
java.lang.ArrayIndexOutOfBoundsException:length=1;此时将显示索引=1

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


注意:我尝试了此()解决方案,但没有处理我的案例。

splitByTitikDuaVCard
可能具有
长度=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

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

替换这个

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

mapContentTerbaruVCard.put("key"+y, splitByTitikDuaVCard[y]);
在此代码中,您的密钥将是key0、key1等。


尝试此代码,它将帮助您

错误告诉您ArrayList的大小为1。因此,唯一元素的索引为0。“日志显示索引1存在”请显示此日志。另外,您应该给出一个完整的代码示例。确保将代码包含在类中的方法中。最后,发布整个stacktrace,并向我们显示导致错误的行。数组从0开始计数。基本上,错误消息告诉您的是:您有一个数组/arraylist,它有一个元素,您试图访问索引1处的元素,该元素实际上是列表中的第二个元素。(或者不是因为你出错了)。说“我已经试过了…”是不够的。您的代码永远不会与链接中给出的代码完全相同。这意味着您需要准确地向我们展示您所尝试的内容,以便我们能够从中帮助您。请参阅我上面的第二条评论。但我已将其拆分为
,它应该包含2个索引,因此
索引0
索引1
,请告知我。@Koma看到更新的答案,它将解释可能发生的情况。不,先生。我做
String[]splitByTitikDuaVCard=vcard.get(o).split(Pattern.quote(“:”)
System.out.println(“结果长度:+splitByTitikDuaVCard.LENGTH”)长度显示
2
这是不可能的。张贴stacktrace。请参阅更新的答案。日志的最后一条记录显示长度为1。所以我猜是对的,“钥匙”到底是什么意思?。在我的案例中,我需要将
splitByTitikDuaVCard
索引0
保存为
hasmap
中,该键是
HashMap
中值的标识。您不知道
HashMap
中的值,但是您知道键,然后您可以很容易地找到特定键的值。是的,将索引存储为键,以便从索引中获取值。但不要存储硬编码值。再看看我的答案。
mapContentTerbaruVCard.put("key"+y, splitByTitikDuaVCard[y]);