And then we can also print the ArrayList as
it is now. There are two different ways. ArrayList has also a toString method so I could directly
say System.out.println(elements) but I am doing it a bit more complicated because I want to show you how we actually iterate
over the ArrayList and this is because ArrayList and every collection implements the iterable
interface which is used in this new for each loop and so we can iterate over the ArrayList
by saying String str and then colon and then elements. And what is required to iterate
over this list will be done here inside and you don’t see it. This is all done by the
compiler for us internally. In short I can tell you there is an integrator method that
I showed you shortly before which will then be used. You might also check my last episode
where I introduced you to the iterable and the interator interface, both will be used
here. For now, it’s enough if you see you can use the ArrayList in the for each loop
and now we can print out each element separately, which of course gives us a bit more flexibility
like I could print something else here also. So this is called iteration going over the
collection element by element and I also want to show you, which is a feature of list, this
order how we added the elements A, B, A, C, E, E will be also exactly the same order as
the elements I printed out which would be different if we had a set.
But enough of talking, let’s already execute it and see it in action. So here you see A,
B, A, C, E, E. Exactly like we added that, which is a feature of list. Now that we added
elements, we could also remove some elements which are also just as easily removed. And
now on the collection interface, I can’t remove by index but I can remove by stating
the object that I want to be removed which by the way internally uses the equals method.
If you don’t know about the equals method, I also have a video about the equals method.
So it goes over each element and checks by using equals which is implemented in the string
class if this A is equal to one of those elements here and we’ll then remove it. You might
also notice I have two A’s, so now it’s interesting to know will the first A be removed,
will the second A be removed or will both be removed. And there is another tip I have
for you, if you have such questions, you can look for a book, for a video tutorial, you
can look in the API and so on and so forth, but you will never be as sure and probably
also never be as fast if you just write a simple test like this and you just try it
out for yourself. How pragmatic? How cool is that? Because the code doesn’t lie, if
you see it, you will be assured that it behaves like you can see it. So let’s do this and
check which element is removed. And here you see B, A, C, E, E, so the first A is missing,
so the answer is the first element is removed, not all A’s are removed.
So what else can we do? Let’s check! We can also ask for the size. Let’s make this
a bit simpler now to have it as short as possible. So now we have one, two, three, four elements.
I’ll copy that. Copying some code that you will reuse is just safer not to introduce
any typo. And as long as you are just copying the code and not duplicating the code, it’s
also fine. So this supposed to give us the number of elements. Let me document this out
so we just see the number of elements. Let’s also do it directly after instantiation of
the object so I expect here to see a zero, no elements included. I mean, this would be
another alternative possibility that this would throw an exception if the list is empty
so let’s check that. I execute the test. It was actually not really a test I am just
experimenting. So you see it returns zero in the first line and then four, so this is
the size method. You can even ask are you empty? Which should probably here say yes,
true, and here probably false. And there it is, true and false.
So let’s now switch over to the list interface and let’s also play a bit around with that.
You will see I have now a reference variable of type list and I can use all the methods
exactly like I used them before because the list interface extends the collections interface
so it includes all the methods that the collection interface has, but on top of that, we now
have more methods than just the methods of the collection interface. For example I can
insert an element by giving an index position. So I could for example say at index position
0 add me T and at S add index position 2. And then let’s use the simple and fast printout
method. Let’s remove this because it will format the list in a one liner but let me
just show you what I mean. We still have the true, we can also remove this, but for now
it’s fine. So we have T because we added that at index position 0 which means this
will be inserted where A was before, before I added that at index 0, this was the index
0, so let’s put it to where A was before and then shifted everything else to the right.
And this is why now this is nice to see it like this in one line because you will see
this has all shifted to the right which when I remove an element, the opposite happens
like if I remove the S, everything will be shifted to the left but let’s see this later.
And we added the S at index 2 so this is now index 0, 1 and 2 which again shifted the rest
to the right. So let’s also remove something. Let’s do this here. Let’s say I removed
index and now let’s see, I remove index 1 which should be the B. And there it seems I am wrong because the
A is still there, the B is still there, let us see. Well of course because it’s a bit
confusing, as I added the T at index position 0, A is now index position 1 and this one
was removed. So removing by index is always not so easy. Really, I have to tell you these
index-based methods I hardly ever use them. Normally you will find actually with most
of the methods of the collection interface which again tells you often it’s a good
idea to use collection here. So this actually inserts an element.
By the way, let’s increment this a bit just for demonstrations purposes. This is not so
flexible. Really, as I said in my slides, you should really think carefully the number
of elements that you need. Just here it’s different because we are playing around. Because
I want to show you the difference again in the example between the capacity and the size
because the size of the list is the number of elements, let’s do this here, and not
the capacity, because my array with a capacity of 100, the array will be 100 elements big
even though they are empty but the size, however, will be much smaller. And here you see the
size is 4 and not 100 so there is a difference. Now we can also ask for the index of a certain
element. Let’s do this here. IndexOf and there is also last indexOf and also isEmpty,
this we should also use. So indexOf, let’s use the A. Maybe we should remove that to not make it