Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
List and ArrayList | Basic Data Structures
course content

Conteúdo do Curso

Java Data Structures

List and ArrayListList and ArrayList

Let's begin our introduction to a data structure called List. Above is a flowchart showing us the Hierarchy of Collections and Lists. We can see that it's quite a large and complex diagram, but let's break it down step by step. In this section, we'll get acquainted with a type of collection called lists. We'll explore list types such as ArrayList, LinkedList, and Stack.

Note

We won't be discussing Vector, as it is an outdated data structure that ArrayList has entirely replaced.

Because all Lists inherit from the List interface, we can obviously create objects through the parent interface. But more on that later. Let's talk about the first type of list - ArrayList.

Above, you see the syntax for creating an ArrayList<>. There is one unfamiliar thing for you in this syntax - it's the generic. We will talk about generics and how to create them later. For now, remember that we are creating an ArrayList and specifying the data type that will be stored in this list inside diamant brackets. Remember that lists cannot work with primitive data types. For this, we use wrapper classes.

Let's create a list in the code and try adding a few objects to the list using the add() method.

java

main.java

As you can see, the add() method is extremely simple. It simply adds the value specified in the parameters to the list. Also, note that we must import List and ArrayList from the java.util library. However, IntelliJ IDEA automatically imports these libraries for you, so don't worry about it.

Let's take a look at an example of creating a list with our custom data type:

java

main.java

As you can see, we created our own class Dog and created an ArrayList with this data type. When printed, we obtained a complete list of all objects in the list.

What do we do if we have an array and want to convert it into a list? We can do that using the Arrays class method - asList(). With this method, we can create a list that will contain the same elements as the original array. This is often useful when we need to use list methods on an array. The syntax is quite simple:

And we can immediately use it when initializing a new list, for example:

java

main.java

Just as we can turn an array into a list, we can turn a list into an array using the toArray() method.

ArrayList<> Methods

Let's go through the basic methods for working with a list. Up until this point, we could easily replace lists with arrays, but let's look at the main difference - the methods.

The add() method is an overloaded method and has a second usage: We can add an element at a specific index.

Note

Lists, like arrays, have zero-based indexing.

When we add an element in the middle of the list, all the elements to the right shift by one position to make room for the new element. For example, when adding an element to the beginning of the list, all the elements shift one position to the right. Let's look at an example:

java

main.java

The process of how the values change can be seen in the illustration:

Obviously, if we can add objects to the list by index, we can also retrieve objects from the list by their index using the get(int index) method. Let's look at an example:

java

main.java

We can add, we can retrieve, but can we delete? Certainly, we can! Using the remove(int index) method. This method is also overloaded, so there is another way to remove an element from the list. We can use the remove(Object obj) method and delete the first occurrence of the specified element.

Let's imagine that we have a large list of all the students in our course. But one of them misbehaved and was expelled. We don't know under which index he was recorded, so we must remove him from the list of students. But there's a catch: the code is a bit broken, and his name may have appeared in the list several times. Let's write a program that will remove all mentions of this student from the list:

java

main.java

You might have correctly noticed that we used another list method - contains(Object obj), which checks whether the specified element is present in the list and returns true or false. Using this method, we set the condition for the while loop execution, thus removing all mentions of a student like "Bob" from the list. Wonderful.

By the way, we could significantly simplify solving this problem using the removeAll(Collection<?> c) method:

java

main.java

The removeAll() method takes a collection as a parameter containing values that it will remove from the collection on which this method was called. In this way, we created a collection of expelled students and added "Bob" to it. This allows us to easily expand our code by simply adding new troublemakers to the collection of expelled students over time.

Let's move back on to the list methods; now we have the update method in line. Considering that the add() method simply shifts all values to the right after itself, we need to figure out how to update a value by index. The set(int index, Object value) method performs this action:

java

main.java

Here it's straightforward; we replaced the element at the second index with the number 28.

Other Methods

We could talk about various methods for working with this list for a long time, but it would take a lot of time. Therefore, you can find all the methods you need with complete documentation and explanations of their operation by opening the ArrayList class in IntelliJ IDEA. To do this, simply import the ArrayList library, hold down the command key (ctrl for Windows), and click on ArrayList. Here, you'll see all the fields and methods of this class, allowing you to familiarize yourself with the methods you might have missed.

1. Which method can we use to add an element by index?
2. Which method can we use to retrieve an element by index?
3. Which method can we use to remove the first occurrence of an element?
4. Which method can we use to replace an element by index?
5. Which method can we use to check for the presence of an element?
6. Can we create a list from an array?
7. How can we create a list from an array?

Which method can we use to add an element by index?

Selecione a resposta correta

Which method can we use to retrieve an element by index?

Selecione a resposta correta

Which method can we use to remove the first occurrence of an element?

Selecione a resposta correta

Which method can we use to replace an element by index?

Selecione a resposta correta

Which method can we use to check for the presence of an element?

Selecione a resposta correta

Can we create a list from an array?

Selecione a resposta correta

How can we create a list from an array?

Selecione a resposta correta

Tudo estava claro?

Seção 1. Capítulo 2
course content

Conteúdo do Curso

Java Data Structures

List and ArrayListList and ArrayList

Let's begin our introduction to a data structure called List. Above is a flowchart showing us the Hierarchy of Collections and Lists. We can see that it's quite a large and complex diagram, but let's break it down step by step. In this section, we'll get acquainted with a type of collection called lists. We'll explore list types such as ArrayList, LinkedList, and Stack.

Note

We won't be discussing Vector, as it is an outdated data structure that ArrayList has entirely replaced.

Because all Lists inherit from the List interface, we can obviously create objects through the parent interface. But more on that later. Let's talk about the first type of list - ArrayList.

Above, you see the syntax for creating an ArrayList<>. There is one unfamiliar thing for you in this syntax - it's the generic. We will talk about generics and how to create them later. For now, remember that we are creating an ArrayList and specifying the data type that will be stored in this list inside diamant brackets. Remember that lists cannot work with primitive data types. For this, we use wrapper classes.

Let's create a list in the code and try adding a few objects to the list using the add() method.

java

main.java

As you can see, the add() method is extremely simple. It simply adds the value specified in the parameters to the list. Also, note that we must import List and ArrayList from the java.util library. However, IntelliJ IDEA automatically imports these libraries for you, so don't worry about it.

Let's take a look at an example of creating a list with our custom data type:

java

main.java

As you can see, we created our own class Dog and created an ArrayList with this data type. When printed, we obtained a complete list of all objects in the list.

What do we do if we have an array and want to convert it into a list? We can do that using the Arrays class method - asList(). With this method, we can create a list that will contain the same elements as the original array. This is often useful when we need to use list methods on an array. The syntax is quite simple:

And we can immediately use it when initializing a new list, for example:

java

main.java

Just as we can turn an array into a list, we can turn a list into an array using the toArray() method.

ArrayList<> Methods

Let's go through the basic methods for working with a list. Up until this point, we could easily replace lists with arrays, but let's look at the main difference - the methods.

The add() method is an overloaded method and has a second usage: We can add an element at a specific index.

Note

Lists, like arrays, have zero-based indexing.

When we add an element in the middle of the list, all the elements to the right shift by one position to make room for the new element. For example, when adding an element to the beginning of the list, all the elements shift one position to the right. Let's look at an example:

java

main.java

The process of how the values change can be seen in the illustration:

Obviously, if we can add objects to the list by index, we can also retrieve objects from the list by their index using the get(int index) method. Let's look at an example:

java

main.java

We can add, we can retrieve, but can we delete? Certainly, we can! Using the remove(int index) method. This method is also overloaded, so there is another way to remove an element from the list. We can use the remove(Object obj) method and delete the first occurrence of the specified element.

Let's imagine that we have a large list of all the students in our course. But one of them misbehaved and was expelled. We don't know under which index he was recorded, so we must remove him from the list of students. But there's a catch: the code is a bit broken, and his name may have appeared in the list several times. Let's write a program that will remove all mentions of this student from the list:

java

main.java

You might have correctly noticed that we used another list method - contains(Object obj), which checks whether the specified element is present in the list and returns true or false. Using this method, we set the condition for the while loop execution, thus removing all mentions of a student like "Bob" from the list. Wonderful.

By the way, we could significantly simplify solving this problem using the removeAll(Collection<?> c) method:

java

main.java

The removeAll() method takes a collection as a parameter containing values that it will remove from the collection on which this method was called. In this way, we created a collection of expelled students and added "Bob" to it. This allows us to easily expand our code by simply adding new troublemakers to the collection of expelled students over time.

Let's move back on to the list methods; now we have the update method in line. Considering that the add() method simply shifts all values to the right after itself, we need to figure out how to update a value by index. The set(int index, Object value) method performs this action:

java

main.java

Here it's straightforward; we replaced the element at the second index with the number 28.

Other Methods

We could talk about various methods for working with this list for a long time, but it would take a lot of time. Therefore, you can find all the methods you need with complete documentation and explanations of their operation by opening the ArrayList class in IntelliJ IDEA. To do this, simply import the ArrayList library, hold down the command key (ctrl for Windows), and click on ArrayList. Here, you'll see all the fields and methods of this class, allowing you to familiarize yourself with the methods you might have missed.

1. Which method can we use to add an element by index?
2. Which method can we use to retrieve an element by index?
3. Which method can we use to remove the first occurrence of an element?
4. Which method can we use to replace an element by index?
5. Which method can we use to check for the presence of an element?
6. Can we create a list from an array?
7. How can we create a list from an array?

Which method can we use to add an element by index?

Selecione a resposta correta

Which method can we use to retrieve an element by index?

Selecione a resposta correta

Which method can we use to remove the first occurrence of an element?

Selecione a resposta correta

Which method can we use to replace an element by index?

Selecione a resposta correta

Which method can we use to check for the presence of an element?

Selecione a resposta correta

Can we create a list from an array?

Selecione a resposta correta

How can we create a list from an array?

Selecione a resposta correta

Tudo estava claro?

Seção 1. Capítulo 2
some-alt