2

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

2
  • You have to actually create the individual ArrayList objects. The new ArrayList[count] operation only creates the array itself.
    – Hot Licks
    Mar 30, 2014 at 17:52
  • Can you please elaborate your answer with code?
    – airbourne
    Mar 30, 2014 at 17:53

2 Answers 2

4

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>();
}
0
2
int size = 9;
ArrayList<SMS>[] lists = new ArrayList[size];
for( int i = 0; i < size; i++) {
    lists[i] = new ArrayList<SMS>();
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.