ArrayLists and arrays
Arrays andArrayList
s are examples of structured data types: they can store multiple elements in order, and the elements can be accessed by their indices. Earlier in the course we encountered Strings. A String is similar in that it contains multiple elements (the characters) and they can be accessed by their indices (using the charAt
method).
However, unlike Strings, arrays and ArrayList
s are mutable, that is, their contents can be modified after construction.
Here is a brief comparison of some of the features of arrays and ArrayList
s.
ArrayLists
- Created using the
new
operator. The type of data it holds goes in the
angle brackets. Example:
ArrayList<String> list = new ArrayList<String>();
- An
ArrayList
can be created to hold any type of Java object (but not primitive types; we have to use the wrapper classes such as Integer
instead)
- A newly created
ArrayList
has length 0.
- The length of the list will expand as necessary as elements are added.
- The length of the list can be obtained using the method
size()
.
- New elements are added to the end of the list using the
add(element)
method.
- The element at index
i
can be accessed using the get
method:
String s = list.get(i);
- The element at index
i
can be changed using the set
method:
list.set(i, "foo");
- The
ArrayList
has a large number of useful methods in addition to add
, get and get
. Some useful ones include contains
, remove
, addAll
, and sort
.
Check out the
ArrayList API documentation
Arrays
- Created using the
new
operator. The type of data it holds goes before the square brackets. Example:
String[] arr = new String[5];
- An array can be created to hold any type of data, including primitive types.
- The length must be specified when the array is constructed. A newly created array of primitive type contains all zeros. A newly created array of objects contains all
null
s.
- The length can be obtained by accessing the
length
attribute (without parentheses).
- New elements cannot be added, since the length is fixed.
- The element at index
i
can be accessed using the bracket notation:
String s = arr[i];
- The element at index
i
can be changed using the bracket notation:
arr[i] = "foo";
- There are no useful methods that are part of an array type. You can, however, find some static utility methods in the
Arrays
class, including toString
, copyOf
, and sort
. See the
Arrays API documentation
new
operator. The type of data it holds goes in the
angle brackets. Example:
ArrayList<String> list = new ArrayList<String>();
ArrayList
can be created to hold any type of Java object (but not primitive types; we have to use the wrapper classes such as Integer
instead)
ArrayList
has length 0.
size()
.
add(element)
method.
i
can be accessed using the get
method:
String s = list.get(i);
i
can be changed using the set
method:
list.set(i, "foo");
ArrayList
has a large number of useful methods in addition to add
, get and get
. Some useful ones include contains
, remove
, addAll
, and sort
.
Check out the
ArrayList API documentation
new
operator. The type of data it holds goes before the square brackets. Example:
String[] arr = new String[5];
null
s.
length
attribute (without parentheses).
i
can be accessed using the bracket notation:
String s = arr[i];
i
can be changed using the bracket notation:
arr[i] = "foo";
Arrays
class, including toString
, copyOf
, and sort
. See the
Arrays API documentation