Buzz Code
Published in

Buzz Code

Java 8 | ArrayList

ArrayList — Re-sizable array that java.util package provides.

Hi y’all! Today we’re going to take a look at the ArrayList. You would remember arrays, there are one dimensional and two dimensional arrays.

Basically all of them(one, two dimensional arrays, ArrayList) have the same purpose, which is storing elements.


ArrayList can be slower than the arrays sometimes, but it’s undeniably easier than that. You don’t have to fix its size when you declare it, and when you remove one of the index, it reorganize the array by itself! I’ll show you an example.

I declared the ArrayList that has Strings in it above. As you can see its basic declaration is like below.

Data type in the second angled brackets<> is optional, so you don’t have to write it but sometimes it’s necessary in the old versions of Java.

(1) add() method

To put elements in the ArrayList you can use the add() method.

Add() method need a parameter which is the element that you want to put in the ArrayList.

(2) get() & size() method

I put few more words in the ArrayList, and used the get() method to get the value at the index 3. So the index will be the parameter of the get() method, and it has a return type which is the value of that index and it’s String in this case.
And then I used the size() method to check how many elements this ArrayList has. I put 5 words, so it returned 5 as its return value.

(3) remove() method

Also you can use the remove() method to delete the certain index. I put number 2 as a parameter of the remove() method, So the word “grape” is deleted from the list.

When you delete one of the index, then it reorganize its indexes like below.

As you see, the values in the index 3 and 4 moved to the index 2 and 3, and its size has also been changed from 5 into 4!

And that’s all for today guys! Thank you for reading my post, I’ll bring some practice questions about the ArrayList next time. See ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store