Answer:
ArrayList<Student> students = new ArrayList<Student>( 30 );
Rough estimates are good enough. It is better to overestimate the required capacity than to underestimate it. A few unused slots do not hurt.
Capacity and Size
An ArrayList object has a capacity and a size.
The capacity is the number of cells that are currently in the array.
The size is the number of cells that are filled.
To find out the current size of an ArrayList use its
size() method.
- The capacity is the number of slots available.
- The size is the number of slots that have data in them.
- Slots 0 up through size-1 have data in them.
- Data are added in order. Before slot N gets data, slots 0, 1, 2, ... N-1 must hold data.
The size increases by one each time an element is added.
However, the capacity remains unchanged until the ArrayList is full.
When an element is added to a full list,
the Java runtime system will greatly increase the capacity of the list
so that many more elements can be added.
QUESTION 8:
(Thought Question: ) Why does the capacity increase by a large amount when an element is added to a full list? Why not just increase the capacity by one?