What is an ArrayList in Java?

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

This is one of the articles from our Java Tutorial for Beginners.


Do you already know how to create arrays in Java? Then it's the right time to learn what an Arraylist is. Let's start!

Let's suppose, you work as a programmer for a logistics company, delivering goods all over the world. You have to make a list of cities where the company delivers the goods.

You can do it in 2 ways:

1. With the help of an array

2. With the help of an Arraylist

Let's have a look at each way. Then it will be clear why the Arraylist is the best friend of the Java developer.

1. WITH THE HELP OF AN ARRAY

Let's create an array with the name deliveryCities. In the curly brackets we have to write the elements' values of the array, i.e. the names of the cities. For example:

As a result, the array called deliveryCities will be created for 4 elements. If we want to get the 3rd element of the array, we have to write deliveryCities[2], since the 3rd element has the index number 2. With this, we can create an array with elements of any type and then work with these elements.

But What if We Would Like to Add One More City, For Example, London?

"Oh, it's not a problem!" - you will tell me. "You simply have to increase the array elements from 4 to 5."

And here's my answer: "You can't do it in such a way, because in Java there is no possibility to change the length of an array. That means, if an array was initially created for 4 elements, now this array will have always 4 elements."

So what can we do? We can try to contrive and do the following - create a new array, this time not for 4 but for 5 elements. Then we have to copy all 4 elements from the previous array to the new array and add one more city - London.

The solution will look like this:

To sum up:

  • First, we created a new array called deliveryCities2, which is 1 element longer, than the 1st array.
  • Second, we copied all elements of the 1st array into the deliveryCities2 array.
  • Third, we set London as the value of the last element of the deliveryCities2 array.

As you see, such approach is not really convenient. Moreover, we had to create the 2nd array to add only 1 additional element. There is a much more convenient solution with the help of an Arraylist.

2. WITH THE HELP OF AN ARRAYLIST

What is an Arraylist? To make it simple, Arraylist is a list that is very similar to an array.

Ok, what's the difference between an array and arraylist?

  • When you create an array, its length is fixed at creation and you can no longer change it.
  • When you create an arraylist, its length is flexible. You can change it as many times as you'd like.

Also an arraylist has a number of methods to add, delete or replace elements of the arraylist at anytime.

And that means that, for example, initially you can create an arraylist for 4 elements and then later add 7 more and then you can even delete 2 elements, and one more time add additional 15 elements, etc. You've got the idea, an arraylist is similar to rubber, you can increase or decrease its length without any problems.

As you remember, in the article "Java - Variable Types. How to Create a Variable in Java", we discussed that there are 8 primitive variable types in Java. Arraylist does not belong to these 8 primitive variable types. Arraylist belongs to an Object type, that is a part of Java Core, namely java.util.ArrayList.

Working with an Arraylist is Really Simple

First of all, you have to create a list. You have 2 options. The most popular choice is to use the following structure:

For our example with cities:

Firstly, as you can see, we wrote a word "Arraylist."

Secondly, since we want to save strings as elements of the Arraylist, we wrote "String" in the angle brackets.

 

Please be attentive: Arraylist cannot work with the primitive variable types in Java.

 

 

Therefore, when working with an Arraylist, if you are using primitive variable types in Java, you would need to apply the wrapper classes for each type accordingly. That's why:

  • for the int type you have to apply Integer wrapper class
  • for the double type you have to apply Double wrapper class
  • etc.

Ok, We've Made the List, but it is Empty. How Can We Add Elements to it?

It's very easy to do. Arraylist has a few methods. To add an element, you have to use a method called add(T element). In the parentheses we would need to write an element.

Thus, to create a list and add 4 elements into it, we have to write the following:

Please note, that when we created the list, we did not specify its length. The length of the list is automatically adjusted to the number of elements. If we decided to delete some elements or add additional elements, the length of the list would be adjusted to the new numbers of elements.

As you remember, to measure the length of an array we used a property "length". To measure the length of an Arraylist, we have to use not a property, but a method called size(). That's why for our list, we write the following:

As a result, the number 4 will be displayed in the console, since our list consists of 4 cities.

So, you already know how to add an element to the end of an Arraylist.

But How Can We Add an Element inside a List?

Suppose we have 3 elements in an list: Chicago, New York and Toronto. And we want to add Paris after Chicago and before New York.

If we worked with an array:

1. We would have to create a 2nd array whith the length longer than 1st array by 1 element.

2. Then with the help or the "for" loop, we would copy all elements of the 1st array to the 2nd array. Moreover, on each iteration we would have to check the name of a city. If the name of a city was Chicago, we would have to paste Paris after Chicago.

As you understand, the code would be fairly long.

With an Arraylist you can do it much more simply.

To add an element to an Arraylist, you have to use a method called add(int index, T element). As you can see, this method has 2 parameters:

int index - in this parameter we write an index for an element, after which a new element can be added

T element - in this parameter we write an element which can be added

In this case the code would look like this:

And how to add an element to the beginning of a list? You can do it by writing this line of code:

Ok, great! Now we know how to add an element to any place of an Arraylist.

How Can We Get an Element under a Certain Index Number? For Example, with the Index Number 2.

If we worked with an array, we would write an index number in the brackets. For example:

When working with an Arraylist, we cannot use brackets. We have to use a method called get(int index). Simply write an index number of an element which we want to get in the parentheses.

In this case, the code would look like this:

How to Get an Index Number of an Element?

We have to apply another method called indexOf(Object o). Simply write an element value of which we want to get the index number in the parentheses.

  • If there is such an element value in the list, the method returns its index number.
  • If there is no such element value, the method returns "-1".

Here's an example:

You've got the idea that by using the indexOf() method you check an element value availability in an Arraylist. Moreover, there is another method called contains(Object o), which:

  • returns "true", if there is such an element value in an Arraylist
  • returns "false", if there is no such element value

Please note, that element values in an Arraylist can repeat a few times (for example, 2 "New York" cities in an Arraylist). So if you need to know how many times the same city repeats in an Arraylist, you can do it with a loop, comparing each element with an element you are searching for, or you can use special classes for this case.

How Can We Replace an Element with Another Element?

Suppose, we have an Arraylist with 3 cities: Chicago, New York, Toronto and we want to replace Toronto with Berlin. To do this, we can use a method called set(int index, T element):

int index  - in this parameter we write an index number of an element which we want to replace with another value

T element - in this parameter we write a new value of an element

How Can We Delete Elements in an Arraylist?

To delete an element, we have to use a method called remove().  This method has 2 options:

  • remove(int index)  - simply write an index number of an element which you want to delete;
  • remove(Object o)  - simply write an element value you want to delete. Please note: if there are a few identical element values, only the first element value will be deleted. To delete all identical element values, you have to use a loop.

Example:

If we want to delete all elements of an Arraylist, we have to use a metod called clear().


You can find more articles in our Java Tutorial for Beginners. 

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr

Facebooktwittergoogle_plustumblrFacebooktwittergoogle_plustumblr
Самоучители--узнать детальнее--