ArrayList Java Tutoriral

Java


In this article, we show how to work with ArrayLists in Java.

ArrayLists in Java can be seen as dynamic arrays.

Unlike conventional arrays, ArrayLists are not set in the amount of elements that the array can hold. In a regular array, in Java, you have to declare how many elements the array holds initially. And once this is set, it cannot be changed. However, with an ArrrayList, the number of elements in the array isn't set. You can add elements to the ArrayList as you please. This is why ArrayLists are referred to as dynamic arrays. For this reason, they are used heavily in Java, probably more so than regular arrays.

We show how to declare (create) ArrayLists, add elements to the ArrayList, get the size of the ArrayList, and many more operations.

So we have the following code shown below.



So the above code shows a lot when dealing with ArrayLists.

So in order to declare and use an ArrayList in Java, it is part of the java.util package. Therefore, you can have to include either the line, import java.util.*; (which includes all files of the java.util package) or include the line, java.util.ArrayList;

Without this import line, the code will not work.

We then create a public class named Team.

We then create our main() method, which is where the program begins to execute the code.

Creating an ArrayList

To declare (create) an ArrayList, you have to specify the Wrapper type of the class, whether it's String, Integer, or Character. You cannot declare primitive types in the ArrayList. ArrayLists are declared with the Wrapper classes of those primitives. So to create an ArrayList of Strings, you have to put in in the declaration. To make the ArrayList a list of Integers, you would have to put in the declaration. To create an ArrayList of characters, you would have to put in the declaration. You cannot put int or char, for example, in the declaration. Unlike regular arrays, ArrayLists deal with the wrapper classes of primitives.

So to create an ArrayList of Strings named team, the declaration is shown below.



So we now have an ArrayList of type String (an ArrayList of Strings) named team.

Adding Elements to the ArrayList

To add elements to the array, we take the array name and use the add() method to add elements to the array.

So in this case, with the ArrayList named team, we take team and followed it with a dot and the add() method. Inside of the add method is the String that we want to add to the array.

So in the code below, we add 5 string elements to the team ArrayList.



So you can see that ArrayLists are very dynamic. You don't have to declare a fixed preset values of the number of elements in the ArrayList. You simply create the ArrayList and then you add elements to it as you go along.

Accessing and Outputting the Elements of an ArrayList

Next we show how to access and output the elements of the ArrayList.

To access the individual elements of an ArrayList, you take the name of the ArrayList and invoke the get() method. Inside of the get method, you specify the index number of the array that you want to access.

To output the individual elements of an array, you can use the code shown below.



So the above lines gets the individual elements of the array. You can see how we use the ArrayList name, followed by a dot, and the get() method, with the index of the ArrayList as the argument to the get() method.

So this is how we can access and output any individual element of an array.

Even though you can output all the elements of an ArrayList with the above method, if you are want to output all the elements of an array, there is a more efficient way of doing it.

One way is to use a regular for loop and looping through the complete ArrayList.

This is shown with the following code.

By getting the length of the ArrayList with the size() method, we can know how many elements there are in the ArrayList. Therefore, we can loop through every element in the Array.

We then output each element in the Array in the body of the for loop.

Even though this for loop works very well, there's an even easier way of doing it using an enhanced for loop.

This is shown below.

You can see how this is much simpler than the previous for loop.

Getting the Size of the ArrayList

To get the size (the number of elements in the ArrayList), we use the size() method.

If you take the array name, followed by a dot, and the size() method, you can get the size (the number of elements in the ArrayList).

This is shown below.



In our example, since we have 5 elements in the array, the size of the array is 5.

How to Update Elements in the ArrayList

To update elements in the ArrayList, we use the set() function.

The set() function takes 2 parameters. The first parameter is the element of the ArrayList that you want to update. The second parameter is the new value that you want to set that element to.

This is shown below.

We update the first 3 elements of the array.



So you can see above that we update the values of the first 3 elements of the ArrayList.

How to Delete Elements of the ArrayList

To delete individual elements of the ArrayList, we can use the remove() method.

You take the name of the ArrayList, followed by a dot, and the index number of the ArrayList that you want to remove.

Below we delete all the elements of the array.



So we delete the first element of the array.

Now the second element becomes the first element in the array.

If you now want to delete the second element of the original ArrayList, you would use team.remove(0) again. So it's a little tricker. If you want to delete multiple elements, you would use the remove() method taking in 2 parameters. The first parameter would be in the index you want to start deleting from in the array and the second parameter is the index where you want to stop deleting to. So it deletes the elements from index start to index finish, according to the statement, remove(int fromIndex, int toIndex). So if you want to delete elements the first 4 elements of an ArrayList, the code would be, remove(0,3). This is the equivalent of using the expression, remove(0); 4 times.

So if you are deleting individual elements of the array, you would use the above method.

However, if you want to delete all elements of the ArrayList, there is a more efficient way of doing it using the clear() method.

If you take the ArrayList name, followed by a dot, and the clear() method, all elements in the ArrayList get erased.

This is shown below.



This clears all elements of the ArrayList.

Checking if an ArrayList is Empty

Java has a built-in function IsEmpty() to check if an ArrayList is empty or not.

You simply take the name of the ArrayList followed by a dot and the isEmpty() function to check if it is empty or not.

This is shown below.



So is the isEmpty() function returns a boolean value of either true or false.

Checking to See if an ArrayList Contains a Certain Value

Java also has a built-in function, contains(), so that we can check to see if an array contains a certain value.

So, for example, we can check to see if the ArrayList contains "Michael".

We would use the name of the ArrayList followed by a dot and the contains() function with the String "Michael" as the argument to the contains() function.

This is shown below.



The contains() function returns the boolean value of either true or false.

So these are many of the functions used with Java ArrayLists.


Related Resources

Java LinkedList Tutorial




HTML Comment Box is loading comments...