2012-01-27 4 views
1

길이가 다른 5 개의 ArrayList가 있지만 모든 배열 목록을 연결할 때 모든 배열 목록의 항목이 같은. 항목을 찾을 수 없거나 같은 크기의 배열 목록을 만들 때 null을 포함하려고합니다. 안드로이드에서 이것을 달성하는 방법 ..안드로이드에서 같은 크기의 모든 arraylists 만드는 법

ArrayList에 1 = 25 개 항목 attaylist 2 = 35 개 항목 arraylits 3 = 5 개 항목 ArrayList에 4 = 13 개 항목 ArrayList를 사전에 몇 가지 예를 감사로 저를 도와주세요 (5) = 40 항목.

모든 항목을 하나의보기 (eg.web보기)로 표시 할 때 6 번째 항목으로 스크롤 할 때 5 개 항목까지 스크롤 할 수 있습니다. 예외가 발생했습니다. 이것을 극복하는 방법. 제발 도와주세요

여기 내 코드 whre 내가 구문 분석하고 목록에 추가됩니다.

+2

코드를 공유하십시오 .. 이전에 시도한 것은 무엇입니까? – user370305

+0

정확히 어떻게'ArrayList's를 연결하고 있습니까? – Vladimir

답변

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

이 기술적, ArrayList를이 변경 가능한 배열의 종류는 순수 자바 문제 .. 안드로이드되지 ... 호출하려는 인덱스가 arrayList의 크기보다 작은 지 확인하십시오. 연결하려는 경우 그렇지 않으면 연결 아웃 예외입니다 .. 또는 arraylist를 이와 같이 배열로 변환 할 수 있습니다 (데이터 유형 String) :

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

이 당신이 그 예외가 thst를 위해 ArrayList에 충분한 공간이 될 수있어 < mArray

행운

+0

나를 도와 주심에 감사드립니다. 코드를 추가하여 제 코드를 확인하고 제발 도와주세요. – cavallo

1

경우 mArrayList 크기의 요소를 비울 null을 할당합니다. 크기가 10 인 b와 크기가 10 인 b, 크기가 10 인 c와 같은 3 개의 arraylist가 있습니다.

이제 모든 arraylist를 크기 10으로 ArrayList z에 추가합니다. 따라서 IndexOutofBount 예외가 발생합니다.

솔루션은 그래서 당신은 그 세 가지 ArrayList의 크기의 합과 같은 크기로 새로운 ArrayList를 z를을해야한다는 해결합니다. 그리고 세 개의 arrayList 데이터를 해당 arrayList에 추가하십시오.

아래 코드를 사용하여 크기를 ArrayList에 지정할 수 있습니다.

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

희망이 있으면 도움이 될 것입니다.

그렇다면 알려주세요.

즐기십시오. :))

+0

코드를 추가했습니다. 코드를 확인하고 제발 도와주세요. – cavallo

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;