0

I initialize a list, like this:

public List<Edge> edges = new ArrayList<Edge>(8);

After that, I would like to get its size, but following code gives zero.

System.out.println("SIZE: " + edges.size());

There isn't any value change or method call beetwen two lines. What can causes this?

  • 1
    The constructor you use instantiate an array with the given capacity. `size()` gives the number of elements in the list, not the underlying array length. Those things are unrelated. – Alexis C. Apr 25 '15 at 15:57
  • http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#size() – Zhenxiao Hao Apr 25 '15 at 15:59

2 Answers2

1

The parameter to your constructor is the capacity.

The capacity is the number of elements that the underlying array can hold before it needs resizing.

folkol
  • 4,441
  • 20
  • 25
0

The size of a List is the amount of elements in it, not the size you initialized it with.

Distjubo
  • 801
  • 6
  • 18