ArrayList and List are array-like collections.
Unlike arrays, the elements count can be dynamic - there is
no need to specify it.
ArrayList
Package to import
Creation syntax
Example
Output:
Explanation:
- At
line 23
an ArrayList member is declared.
- At
line 30
an ArrayList is created.
- At
lines 43 - 45
elements are added to the ArrayList.
- At
lines 47 - 50
all elements of the ArrayList are looped.
List
Packages to import
Creation syntax
Example
Output:
Explanation:
- At
line 24
a List member is declared.
- At
line 31
a List is created.
- At
lines 44 - 46
elements are added to the List.
- At
lines 48 - 51
all elements of the List are looped.
ArrayList VS List
Differences:
List
- List is an Interface.
- List cannot be instantiated.
- equals method can be applied (covered in one of the next lessons).
ArrayList
- ArrayList is a Class.
- ArrayList can be instantiated.
- equals method cannot be applied (covered in one of the next lessons).
Similarities:
- compareTo method can be applied for both List And ArrayList (covered in one of the next lessons).
- Both of them can have dynamic elements count.