0

嗨,我一直在关注一个在线示例,但该网站已关闭(典型)。我在用我创建的对象填充我的数组列表时遇到了麻烦,我不确定我是否错过了示例中的某些内容。

问题是我的日志每次都会产生 0 大小?

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

为任何帮助欢呼。

4

3 回答 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)

于 2012-04-18T10:29:05.960 回答
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/

于 2012-04-18T10:32:39.610 回答
0

您还没有将对象添加到集合中,通过实例化对象并不意味着它们将被添加到列表中。

add() 方法用于将对象添加到列表中。

所以,应该使用这个:m_excersizes.add(e1); m_excersizes.add(e2);

于 2013-10-03T14:44:49.813 回答