Java 如何在android中创建大小相同的所有ArrayList
我得到了五个长度不同的ArrayList,但是当我连接所有数组列表时,我得到了一个数组索引越界异常,因为所有数组列表中的项都不相同。我想在找不到项时包含null,或者使数组列表具有相同的大小。如何在android中实现这一点。。请帮我举一些例子,并提前表示感谢 arraylist 1=25项 附件列表2=35项 arraylits 3=5项 arraylist 4=13项 arraylist 5=40项 当我在一个视图中显示所有项目时,例如web视图,当滚动到第6个项目时,我可以滚动到5个项目,我得到异常。如何克服这个问题。请帮忙Java 如何在android中创建大小相同的所有ArrayList,java,android,arraylist,Java,Android,Arraylist,我得到了五个长度不同的ArrayList,但是当我连接所有数组列表时,我得到了一个数组索引越界异常,因为所有数组列表中的项都不相同。我想在找不到项时包含null,或者使数组列表具有相同的大小。如何在android中实现这一点。。请帮我举一些例子,并提前表示感谢 arraylist 1=25项 附件列表2=35项 arraylits 3=5项 arraylist 4=13项 arraylist 5=40项 当我在一个视图中显示所有项目时,例如web视图,当滚动到第6个项目时,我可以滚动到5个项目,
这是我解析并添加到列表中的代码。如果发布了代码,则更容易看出问题所在。但是,如果您希望在获得越界异常时返回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
}
这是一些关于异常处理的介绍性材料,以备您查看。如果您的代码被发布,则更容易看出问题所在。但是,如果您希望在获得越界异常时返回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
}
这是一些关于异常处理的介绍性材料,以备您查阅。我不知道,但看看这是否有帮助:
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);
}
}我不知道,但看看这是否有帮助:
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);
}
}这不是android。。这纯粹是java的问题 从技术上讲,arraylist是一种可变数组。。。 只需检查您正在调用的索引的位置是否小于arrayList的大小。如果是,则进行连接,如果不是,则进行连接。如果不是这样,则表示您是出界异常。。 或者,您可以尝试将arraylist转换为如下所示的数组:假设数据类型为字符串:
String mArray[]=mArrayList.toArray(new String[SIZE]);
如果mArrayList size祝你好运这不是android。。这纯粹是java的问题 从技术上讲,arraylist是一种可变数组。。。 只需检查您正在调用的索引的位置是否小于arrayList的大小。如果是,则进行连接,如果不是,则进行连接。如果不是这样,则表示您是出界异常。。 或者,您可以尝试将arraylist转换为如下所示的数组:假设数据类型为字符串:
String mArray[]=mArrayList.toArray(new String[SIZE]);
如果mArrayList size祝你好运你得到了一个例外,在这个数组列表中可能没有足够的空间。 Supose有三个数组列表,如a的大小为10,b的大小为10,c的大小为10 现在,您正在将所有arraylist添加到大小为10的arraylist z中,这样总共有30个数据,但索引只有10个。因此它会导致IndexOutofCount异常 解决方案 所以要解决这个问题,你必须得到新的ArrayList z,它的大小就像三个ArrayList大小之和。 然后将三个arrayList数据添加到该arrayList 您可以使用以下代码为ArrayList指定大小
ArrayList<Integer>[size] arrayOfLists = new ArrayList<Integer>[size]();
希望它能帮助你
如果没有,请告诉我
享受 您遇到了一个异常,thst arraylist中可能没有足够的空间。 Supose有三个数组列表,如a的大小为10,b的大小为10,c的大小为10 现在,您正在将所有arraylist添加到大小为10的arraylist z中,这样总共有30个数据,但索引只有10个。因此它会导致IndexOutofCount异常 解决方案 所以要解决这个问题,你必须得到新的ArrayList z,它的大小就像三个ArrayList大小之和。 然后将三个arrayList数据添加到该arrayList 您可以使用以下代码为ArrayList指定大小
ArrayList<Integer>[size] arrayOfLists = new ArrayList<Integer>[size]();
希望它能帮助你
如果没有,请告诉我
享受 在类中创建一些默认值 像
在类中创建一些默认值 像
我建议使用另一个数据结构,并将该项目的列表添加到主图书结构中,新的数据结构可能是:
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;
我建议使用另一个数据结构,并将该项目的列表添加到主图书结构中,新的数据结构可能是:
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;
请分享你的代码。。你之前尝试过什么..你是如何连接你的ArrayList的?请分享你的代码。。你之前尝试过什么..你到底是如何连接你的ArrayList的?谢谢你的帮助,我已经添加了代码请检查我的代码并帮助我请谢谢你的帮助,我已经添加了代码请检查我的代码并帮助我