An ArrayList
and an array are closely related.
A big difference to the usual array is that when creating an ArrayList
, no size has to be specified. The size of an ArrayList
adjusts dynamically.
As with the array, when creating an ArrayList
, you have the option of specifying only the data type of the values, without making an assignment of values.
For the assignment of values, the ArrayList
provides you with the add()
method. The ArrayList
also provides a method for removing values, the remove()
method.
ArrayList <Type> listname = new ArrayList <Type>();
// add element
listname.add("Test");
// remove element
listname.remove("Test");
An iterator allows sequential access to the elements of a collection – for example, an ArrayList
.
Iterator<Type> iter = arraylist_name.iterator();