如何在Java中创建ArrayLists数组? [英] How to create an array of ArrayLists in java?
本文介绍了如何在Java中创建ArrayLists数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个数组,但无法向其中添加值.
I am creating an array but cannot add values to it.
ArrayList<SMS>[] lists = (ArrayList<SMS>[])new ArrayList[count];
for(int i=0;i<temp.size();i++)
{
String number="",id="";
number = temp.get(i).addr;
id = temp.get(i).thread_id;
lists[i].add(temp.get(i)); // Problem here
}
我无法为其增值
推荐答案
您正在创建null
引用的数组,因此您需要将每个引用初始化为new ArrayList<SMS>()
:
You're creating an array of null
references, so you need to initialize each of them to a new ArrayList<SMS>()
:
for (int i = 0; i < count; i++) {
lists[i] = new ArrayList<SMS>();
}
这篇关于如何在Java中创建ArrayLists数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文