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. Commented Mar 30, 2014 at 17:52
  • Can you please elaborate your answer with code? Commented 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>();
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.