A List is an ordered collection, where the user of this interface has control over where in the list each element is going to be inserted. It offers various methods, to add, remove, and look for a specific value in the list.
A List should not be confused as an ArrayList. The first one is a child interface of Collection, and the second one is a class that implements the List interface.
So we can implement the idea of the List, to use an ArrayList, it needs to be imported into the program.
//import the list to make it available
import java.util.ArrayList;
public class Program {
public class void main(String[] args) {
//main program
}
}
Since all the variables stored in a given list are of the same type, we must define the Type in the type of values to be stored.
ArrayList<Type> nameOfTheList = new ArrayList<>();
In actual code, an example to create a list for storing Strings.
//import the list to make it available
import java.util.ArrayList;
public class Program {
public class void main(String[] args) {
//create a String list
ArrayList<String> list = new ArrayList<>();
}
}
Basically, the first part ArrayList<String
is the type of the ArrayList, and the command to create is new ArrayList<>()
.
Type of values that a List can contain
In Java, we have two categories of variables. The first one, is the primitive data type, which are values like int
or double
where they actually hold their values. The second one, is the reference data type, like ArrayList
, where it contains a reference to the location in memory where is the value related to this variable.
This is why we need to declare the Type of the ArrayList
capitalized. The moment that we create new lists, like the ones below.
ArrayList<Integer> list = new ArrayList<>();
list.add(42);
ArrayList<Double> list = new ArrayList<>();
list.add(4.20);
ArrayList<Boolean> list = new ArrayList<>();
list.add(true);
The ArrayList
assumes that all variables in it are reference types. Java automatically converts an int
variable into Integer
. Amazing, right?
So, it is pretty simple to create an ArrayList
. First we define the Type
, and later we assign to create using the new
command.
Interface
The List Interface are zero-based. It means that we start couting by 0. So the first position of an element, is not the number 1, but it is the zero-number. And with the actual knowledge it is easy the way that we can do operations, use methods and retrieve values through their index (position).
To understand this better, let`s keep constructing the program we start creating in the beginning of this article.
`java
//import the list to make it available
import java.util.ArrayList;
public class Program {
public class void main(String[] args) {
//create a String list
ArrayList list = new ArrayList<>();
//add three values
list.add("First");
list.add("Second");
list.add("Third");
//retrieve value through their index (position)
System.out.println(list.get(0));
}
}
`
Can you guess which element will be displayed?
You`re totally right if you guessed First.
First
List Methods
Here, we can verify a bunch of useful methods for the ArrayList
.
- Verify if the ArrayList is empty, returns true if the list contains no element.
ArrayList<String> list = new ArrayList<>();
System.out.println(list.isEmpty());
- Adding to a list is done with the method below.
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(5);
numbers.add(3);
- The size method is straightforward.
ArrayList<String> wordList = new ArrayList<>();
int totalSize = wordList.size();
- To retrieve a value from a certain index, we use the .get method.
ArrayList<Double> decimals = new ArrayList<>();
decimals.add(0.01);
String decimal = decimals.get(0);
System.out.println(decimal);
- Removing elements from a list, it can receives both, a parameter either the value, or the index of the value to be removed.
ArrayList<String> list = new ArrayList<>();
list.remove("hello world!");
list.remove(3);
- Verify the existancec of a value with the contains method, returns true if the list contains the specified element.
ArrayList<String> list = new ArrayList<>();
boolean found = list.contains("hello world!");
Top comments (0)