0

Hi I've been following an online example, but the site is down (typical). I'm having trouble with populating my array-list with the objects I created and I'm not sure if I missed something from the example.

the problem is my log produces a 0 size each time?

m_excersizes = new ArrayList<Excersize>();
Excersize e1 = new Excersize();
e1.setExcersizeOn("no 1");
e1.setExcersizeTitle("title 1");


Excersize e2 = new Excersize();
e2.setExcersizeOn("no 1");
e2.setExcersizeTitle("title 1");

Log.i("ARRAY", "This many: "+ m_excersizes.size());

cheers for any help.

2
  • 1
    you missed adding those objects into arraylist. (it is not automatic) use m_excersizes.add(e1); m_excersizes.add(e2); Commented Apr 18, 2012 at 10:29
  • 1
    because you never add Excersize into your list Commented Apr 18, 2012 at 10:29

3 Answers 3

3

You forgot to add the element to the list: mExcersizes.add(e1);

(Note: underscore-separated variables names are not the accepted convention in Java. Use camelCase)

Sign up to request clarification or add additional context in comments.

Comments

1

You never add anything to your list. Add the following lines:

m_excercizes.add(e1); m_excercizes.add(e2);

And please stick to the Java naming conventions: exercises instead of m_excercizes. Moreover, the setExcercizeTitle() method is part of the class Excersize. Naming it this way is redundant. setTitle() is sufficient.

Here's the link of the documentation of J2SE: http://docs.oracle.com/javase/6/docs/api/

2 Comments

i'm terrible at sticking to documented conventions, but will try, thanks for your guidance.
funny you should bring that up. I know (now) how to spell exercise. But it hurts my fingers every time I have to force them into that sequence! can you believe that. ;) I thought coders could get away with bad spelling. Thanks to you I am now reading Java docs and a dictionary!
0

You haven't added the objects into the Collection, by instantiating object doesnt mean that they would be added to the list.

add() method is used to add the object to list.

So, this should be used: m_excersizes.add(e1); m_excersizes.add(e2);

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.