2017-03-08 65 views
-4

我知道這個問題聽起來很愚蠢,因爲這裏有幾個成員,但我不知道了。Android:無法獲取ArrayList上的索引1

我遇到了一個數組奇怪的問題。我試圖通過ArrayList獲得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 1HashMap,出現錯誤

我的問題是:如何找到index 1這樣我就可以把它保存到hashmap

注:我已經嘗試這個(Java ArrayList IndexOutOfBoundsException Index: 1, Size: 1)解決方案,但沒有工作的我的情況。

+5

該錯誤告訴你ArrayList的大小是1.唯一的索引因此元素爲0. –

+0

「日誌顯示索引1存在」請顯示此日誌。另外,你應該提供一個完整的代碼示例。一定要包括把你的代碼放在類內部的方法中。最後,發佈整個堆棧跟蹤並向我們顯示哪一行會導致錯誤。 –

+0

數組從0開始計數。基本上,錯誤消息告訴您的是以下內容:您有一個數組/數組列表,其中包含1個元素,並且您嘗試訪問索引1處的元素,該元素實際上是列表中的第二個元素。 (或者是因爲你有錯誤)。 –

回答

0

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); 
    } 
} 

Lookint在你的日誌:

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

長度=

+0

但我用':'分割它,它應該包含2個索引,因此'索引0'和'索引1',請告訴我。 – Koma

+0

@Koma查看更新的答案,它會解釋可能發生的事情。 – LppEdd

+0

不,先生。我做'String [] splitByTitikDuaVCard = vcard.get(o).split(Pattern.quote(「:」));''和'System.out.println(「RESULT LENGTH:」+ splitByTitikDuaVCard.length);''length顯示'2' – Koma

-1

在第二環(內環)你是從splitByTitikDuaVCard[1]獲得價值爲什麼你要硬編碼索引的價值。你應該使用y的值。

在此代碼替換此

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

有了這個

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

您的關鍵將是KEY0,KEY1,...

試試這個代碼,它會幫助你

+0

究竟「鑰匙」是什麼意思?我的情況我需要將'splitByTitikDuaVCard'的'index 0'作爲'key'保存到'hasmap'中 – Koma

+0

** key **是'HashMap'中的值的標識。你不知道'HashMap'中的**值**,但你知道密鑰,那麼你可以很容易地找到特定的**鍵**的值。是商店索引作爲一個關鍵,所以你可以從索引中獲得價值。但不要存儲**硬編碼**值。再次看到我的答案。 –