2012-01-27 48 views
1

我弄到了五週的ArrayList不同長度,但是當我串聯所有的數組列表,我得到一個數組索引出界異常,因爲在所有的數組列表中的項目都沒有的相同。我想要在找不到該項目或者使數組列表具有相同大小時包含null。如何在Android中實現這一點..請幫助我做一些例子,在此先感謝如何使相同大小的所有的ArrayList中的Android

的ArrayList 1 = 25項 attaylist 2 = 35項 arraylits 3 = 5項 ArrayList的4 = 13項 ArrayList的5 = 40項目。

當我顯示在一個視圖(eg.web視圖)我可以滾動高達5個項目時,滾動到第6項我得到豁免的所有項目。如何克服這一點。請幫助

這裏是我的代碼whre我解析,並添加到列表中。

+2

請分享你的代碼..你以前試過什麼.. – user370305 2012-01-27 07:07:15

+0

你究竟如何連接你的'ArrayList's? – Vladimir 2012-01-27 07:22:58

回答

2

這是比較容易看到的問題是什麼,如果你的代碼張貼。如果你想,當你得到一個出界異常返回NULL,不過,這樣的事情應該這樣做:

try { 
    //your code that's throwing index out of bounds 

    //this block should include as little of your code as possible 
    //for example, don't put a for loop in here -- 
    // put this inside the for loop if you can. 
} catch (IndexOutOfBoundsException e) { 
    return null; 
    // or do what you need to do with the null 
} 

Here的異常處理的一些介紹材料,以備想一探究竟。

3

我不知道,但看這有助於:

package your.package 
import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class TestProjectActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    TextView tv = new TextView(this); 

    setContentView(tv); 

    ArrayList<Integer> a = new ArrayList<Integer>(); 

    a.add(1); 
    a.add(2); 
    a.add(3); 
    a.add(4); 

    ArrayList<Integer> b = new ArrayList<Integer>(); 

    b.add(5); 
    b.add(6); 
    b.add(7);   

    a.addAll(b); 


    String p = ""; 
    for (int i = 0; i < a.size(); i++) 
    { 
     p = String.format("%s, %d", p, a.get(i)); 
    } 

    tv.setText(p); 
} 

}

2

這不是...的android其純java問題 技術上,ArrayList的是一種可變數組的... 只是檢查你在哪裏調用指數小於的ArrayList的大小,如果是,進入串聯,如果不是這樣的,你是出界例外.. 或者你可以嘗試你的ArrayList轉換成數組像這樣(假設數據類型是字符串):

String mArray[]=mArrayList.toArray(new String[SIZE]); 

這將null分配到空的情況下,mArrayList大小元素< MARRAY

好運

+0

感謝您的幫助,我已添加驗證碼請檢查我的驗證碼並幫助我請 – cavallo 2012-01-27 08:09:06

1

你說的異常有可能是在對THST ArrayList中沒有足夠的空間。 這裏有三個像列表一樣的大小爲10的b,大小爲10的b和大小爲10的c。

現在,您將所有數組列表添加到大小爲10的ArrayList z,然後共有30個數據,只有10個。所以它會導致IndexOutofBount異常。

解決方案

所以要解決,你必須採取新的ArrayList Z間大小一樣,所有三個ArrayList的大小的總和。 然後將三個arrayList數據添加到該arrayList。

,您可以給大小與下面的代碼的ArrayList。

ArrayList<Integer>[size] arrayOfLists = new ArrayList<Integer>[size](); 

希望它能幫助你。

如果沒有,然後讓我知道。

享受。 :))

+0

我已添加驗證碼請檢查我的驗證碼並幫助我 – cavallo 2012-01-27 08:08:46

2

做一些默認值在 類像

public class BOOK { 
String BId=""; 
String BTitle=""; 
String prelimTitle=""; 
String prelimIntro=""; 
String prelimInsight=""; 
} 
2

我建議有另一種數據結構,並添加項目到主簿結構的名單,新Datastructre可能是:

case XmlPullParser.END_TAG: 
     if (xmlpullparser.getName().toString().equals("BOOK")) { 
      book.setInsightList(insightList); 
      book.setcasestudyList(casestudyList); 
      book.setquotesList(quoteList); 
      book.setoneminwonderList(oneminwonderList); 
      book.setsecretList(secretList); 
      bookList.add(book); 
      Intro.book = book; 
     } else if (xmlpullparser.getName().toString().equals("chapter")) { 
          if(objInsight==null) 
           objInsight=new INSIGHT(); 
          listInsights.add(objInsight); 
          objInsight=null; 
     } else if (xmlpullparser.getName().toString().equals("secret")) { 
      secretList.add(secret); 
     } else if (xmlpullparser.getName().toString().equals("prelim")) { 
      secretList.add(secret); 
      this.prelim = false; 
     } else if (xmlpullparser.getName().toString().equals("prelimTitle")) { 
      this.prelimTitle = false; 
     } else if (xmlpullparser.getName().toString().equals("prelimIntro")) { 
      this.prelimIntro = false; 
     } else if (xmlpullparser.getName().toString() 
       .equals("prelimInsight")) { 
      this.prelimInsight = false; 
     } else if (xmlpullparser.getName().toString() 
       .equals("prelimContent")) { 
      this.prelimContent = false; 
     } else if (xmlpullparser.getName().toString() 
       .equals("chapterIntro")) { 
      secretList.add(secret); 
      this.chapterIntro = false; 
     } else if (xmlpullparser.getName().toString().equals("bold")) { 
      this.chapterIntrobold = false; 
     } else if (xmlpullparser.getName().toString().equals("secretIntro")) { 
      this.secretIntro = false; 
     } else if (xmlpullparser.getName().toString().equals("insight")) { 

     } else if (xmlpullparser.getName().toString().equals("caseStudy")) { 
      this.caseStudy = false; 
      casestudyList.add(casestudy); 
     } else if (xmlpullparser.getName().toString().equals("oneMinute")) { 
      this.oneMinute = false; 
      oneminwonderList.add(oneminwonder); 
     } else if (xmlpullparser.getName().toString().equals("quote")) { 
      quoteList.add(quotes); 
     } else if (xmlpullparser.getName().toString() 
       .equals("quoteContent")) { 
      this.quoteContent = false; 
     } else if (xmlpullparser.getName().toString() 
       .equals("quoteCitation")) { 
      this.quoteCitation = false; 
     } else if (xmlpullparser.getName().toString() 
       .equals("secretContent")) { 
      this.secretContent = false; 
     } 

     break; 
相關問題