ArrayList is a pre-made tool in Java that helps dealing with lists. It offers various methods, including ones for adding values to the list, removing values from it, and also for the retrieval of a value from a specific place in the list. The concrete implementations — i.e., how the list is actually programmed — has been abstracted behind the methods, so that a programmer making use of a list doesn't need to concern themselves with its inner workings.
Creating a new list is done with the command ArrayList<Type> list = new ArrayList<>()
, where Type is the type of the values to be stored in the list (e.g. String
).
When defining the type of values that a list can include, the first letter of the element type has to be capitalized. A list that includes int-type variables has to be defined in the form ArrayList<Integer>
; and a list that includes double-type variables is defined in the form ArrayList<Double>
The reason for this has to do with how the ArrayList is implemented. Variables in Java can be divided into two categories: value type (primitive) and reference type (reference type) variables. Value-type variables such as int
or double
hold their actual values. Reference-type variables such as ArrayList
, in contrast, contain a reference to the location that contains the value(s) relating to that variable.
Value-type variables can hold a very limited amount of information, whereas references can store a near limitless amount of it.
Once a list has been created, ArrayList assumes that all the variables contained in it are reference types. Java automatically converts an int
variable into Integer
when one is added to a list, and the same occurs when a variable is retrieved from a list. The same conversion occurs for double
-type variables, which are converted to Double
. This means that even though a list is defined to contain Integer
-type variables, variables of type int
can also be added to it.
Addition is done with the list method add
, which takes the value to be added as a parameter. To retrieve a value from a certain position, you use the list method get
, which is given the place of retrieval as a parameter. List positions are counted starting from zero
If you try to retrieve information from a place that does not exist on the list, the program will print a IndexOutOfBoundsException
error.
The error message provides hints of the internal implementation of an ArrayList object. It lists all the methods that were called leading up to the error. First, the program called the main
method, whereupon ArrayList's get
method was called. Subsequently, the get
method of ArrayList called the rangeCheck
method, in which the error occurred. This also acts as a good illustration of proper method naming. Even if we'd never heard of the rangeCheck
method, we'd have good reason to guess that it checks if a searched place is contained within a given desired range. The error likely occurred because this was not the case.
Each tool offered by Java has a name and location. The program can use a tool after it has been imported with the import
command. The command is given the location and the name of the desired class. For example, the use of an ArrayList necessitates placing the command import java.util.ArrayList;
to the top of the program.
The number of values on a list is provided by the list's size method which returns the number of elements the list contains. The number is an integer (int
), and it can be used as a part of an expression or stored in an integer variable for later use.
If you don't need to keep track of the index as you're going through a list's values, you can make use of the for-each loop. It differs from the previous loops in that it has no separate condition for repeating or incrementing.
In practice, the for-each loop examines the values of the list in order
one at a time. The expression is defined in the following format: for (TypeOfVariable nameOfVariable: nameOfList)
, where TypeOfVariable
is the list's element type, and nameOfVariable
is the variable that is used to store each value in the list as we go through it.
The list's remove method removes the value that is located at the index that's given as the parameter. The parameter is an integer. If the parameter given to remove
is the same type as the values in the list, but not an integer, (integers are used to remove from a given index), it can be used to remove a value directly from the list.
If the list contains integers, you cannot remove a number value by giving an int
type parameter to the remove method. This would remove the number from the index that the parameter indicates, instead of an element on the list that has the same value as the parameter. To remove an integer type value you can convert the parameter to Integer
type; this is achieved by the valueOf
method of the Integer class.
So Integer.valueOf()
can be used to convert int
type to Integer
type.
Basically if you provide remove
with int
it removes the value at that index. Otherwise if a reference type is provided it will remove the values in the ArrayList having the same value as the value of reference type variable.
The list method contains can be used to check the existence of a value in the list. The method receives the value to be searched as its parameter, and it returns a boolean type value (true
or false
) that indicates whether or not that value is stored in the list.
Earlier we have used integers, floating point numbers, etc. as method parameters. When variables such as int
are used as method parameters, the value of the variable is copied for the method's use. The same occurs in the case that the parameter is a list.
Lists, among practically all the variables that can store large amounts of information, are reference-type variables. This means that the value of the variable is a reference that points to the location that contains the information.