airbourne
airbourne

Reputation: 119

How to create an array of ArrayLists in java?

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
        }

I am unable to add value to it

Upvotes: 2

Views: 13627

Answers (2)

Thalaivar
Thalaivar

Reputation: 23632

int size = 9;
ArrayList<SMS>[] lists = new ArrayList[size];
for( int i = 0; i < size; i++) {
    lists[i] = new ArrayList<SMS>();
}

Upvotes: 2

Thomas
Thomas

Reputation: 181745

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

Upvotes: 4

Related Questions