ArrayList: Java Collections Framework Video Tutorial, Part 1

The first part of a series on the Java Collections Framework, an absolutely vital set of classes for organising data in your code. In this part we'll look at ArrayList; an expandable array. ArrayList is probably the most used and easiest to use member of the collections framework.

After starting the video, click the maximise button to make it fullscreen so you can see the code!

Code For This Tutorial

import java.util.ArrayList;
import java.util.List;

public class App {

    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<Integer>();

        // Adding

        // Retrieving

        System.out.println("nIteration #1: ");
        // Indexed for loop iteration
        for (int i = 0; i < numbers.size(); i++) {

        // Removing items (careful!)
        numbers.remove(numbers.size() - 1);

        // This is VERY slow

        System.out.println("nIteration #2: ");
        for (Integer value : numbers) {

        // List interface ...
        List<String> values = new ArrayList<String>();




This is John from and welcome to a series of tutorials on the collections framework in Java. In this tutorial we'll going to start by looking at the probably the simplest and most often used member of the collections framework which is: ArrayList. So, the ArrayList class kinda implements an array that's expandable.

So, I'll type the ArrayList here and all the members of the collections framework, I'll template classes. So, after you'll write the name here of the class you want to use, you always have to specify the type or types of the things that you want to store, in the class, in kind of angle brackets like this. I want to put the int, integers in this ArrayList. I'll call this “numbers” and I'll say “numbers = new ArrayList” and I'll set this equal too. I'll need to put an integer here as well. So, basically wherever I type the name of the class I'll need the kind of type that I want to store in it as well. And, you can, I just to control-shift o the input here, Java to the ArrayList. And you can specify an inten..tional size for your ArrayList at the start, but if you don't, then I think you'll need to full-size????? 10 so normally I don't bother really. And this worst noting there of course in case you not too familiar with template classes, you can't put a primitive type there. You need to use the corresponding non-primitive types or use integer, not int.

Now to add values to an ArrayList it's very simple, there's a method called “add”. So, let's say numbers.add. Let's add 10, like this: “numbers.add(10)”. Then I'll add maybe a 100: “numbers.add(100)” and let's have one more “numbers.add (40)”. Okay. And you can get numbers from this list pretty simple as well, by saying; “numbers.get”, so let's have a sysout and I'll say numbers.get and the get method you just give in the index of the values you want to get. So here I've got a three values, so the index would be as in as in Array: 0,1,2. And let's say I want to get the first item and it would be in index 0. So, if I run that I'll get 10.

And another common thing that you want to do besides adding values and getting values - let's put some comments here; adding and retrieving - is that you want to iterate over all the items in your list and I can do that for example with the for loop. So, this is using indexed for loop iteration. And I can say “for(int i=0; i < )”. And I want “i” to go from 0 and I wanted to be incremented as long as it's not equal to the size of the list or in other words the numbers of the elements in the list. So, there are three elements in this list I want a to go to 0,1,2. And I can get the number of items in the list by just using the dot-size method. So, pretty simple.

And let's have a look at this. So now I can index the list, I can say “numbers.get.(i)”, like that. And that will ??????? my list. So let's put a sysout here. “Iteration #1”, let's call it. And then here I've got all the values in my list.

There is another way to iterate through lists, same thing that works with Arrays, also works with lists. So, I can say “for()” and here here I'll type the type of the thing that is in my list that I want to retrieve, so, Integer in this case. Integer value in numbers. I'll put the colon and the name of my list here. And this will just set value ???? be values in my list in therm. So I can say sysout value. And let's call this “sysout control-space slash n iteration #2”, like that. Okay and if I'll run this I'll get 10, 100 and 40 again which are the values in my list. There is one last thing that I am going to cover in this tutorial... and that's removing items from the list so it ??? cover adding. How can you remove items? Well, you can say there is a remove method and you just specify the index thing that you want to remove. So, if you want to remove the first one, what would be 0 or the last one would be in this case “numbers.size() -1”. But the problem is that you need to be careful when you remove items from an ArrayList because internally there's an ArrayList that stoles ??? kind of an Array. And when you see the size of the Array, it will create a bigger Array and copy the items to that and carry on adding to that. So you can imagine that if you remove items from the end then that's pretty quick because all that would do is kind of ??? the size of the list internally.

But... supposing you remove items from the beginning... this is very slow. So let's say “numbers.remove (0)” so I remove the 0's item and let's maybe put these here so you can see them working. I do control-shift to format. So, okay if I'll run this, so, this is the list to start with 10-100-40. Then I remove the last item 40 and the first item 10 so then I've got a 100 left. But this is slow because when you remove items from an ArrayList what it does it will - if you remove the first item - it will then copy all the items, all the subscan ??? items one step back to fill the whole ??? so that's very slow. If you remove the last item - it doesn't have to do that- , so that's fast. And even if you remove like the second to last or the first to last item that could be reasonable fast. But if you remove the first item that's gonna be slow. And it doesn't matter here, because we've only got you know just three items in the list so we just do will remove them once. But if you had a big list and you're removing items again, again, again from the middle of the list or the beginning of the list then that could really slow down your ???? so you don't want to do that. And if you need to remove items from the middle of start of the list then anywhere, rather than an absolute end really, then you need to use, let's say LinkList for example. And we'll cover LinkList in the next tutorial. One last thing I should mention here is that the kind of collections classes or ??? by interfaces, so all the list objects for instance implement the list interface. So you can often see code that looks like this. Let's say “List values = new ArrayList” I need to put the template types in here as well. Let's make this store strings, like that. And I'll need to put the template type of my interface as well. So, this is just saying this values variable implements can point to anything that implements the list interface. I do control shift a??? like the import as well Java list.
And we'll going to point it actually to an object specifically of the type ArrayList. Because this isn't a class, it's just an interface. So you cant create new lists, only new ArrayLink or new LinkList or whatever. 

Okay, so, that's it for this tutorial and as I say in the next tutorial we'll look at Link list and we'll compare LinkList to the ArrayList, so then you can see why you would want to use LinkList even that actually is a bit more heavy way in uses, apparently more memory than ArrayList, but for most perhapses you want Arraylist.
So, join me again next time and you can find this code on and until next time happy coding.