ArrayLists in Java

Hi there! Welcome back to Java made easy! The ArrayList Class is part of a collection framework in the java.util package that provides users dynamic arrays that they can manipulate. One great benefit of using these arrays is that you do not need to specify the size of the array and even if you do you can always add elements to it. The downsides of using this array is that in only supports object entries and does not support primitive types like int, char,etc. A caveat to consider is that the speed of ArrayLists is slower than that of standard arrays. Do use these primitive types you would have to use a wrapper. which my colleague William elaborates on in his blog and his link is at the bottom.

So to use the class ArrayList one must first import it it from the java.util library.

import java.util.ArrayList;

To declare an array object of the ArrayList class you must use the keyword ArrayList and define the data type of the elements that will populate it and then what we will be naming our array. In this case my array will be called lunch and we will instantiate it as such:

  ArrayList<String> lunch = new ArrayList<String> (2);
//notice that I specify a size of 2. This space inside this parenthesis can be empty when instantiating.

So now that we know how to instantiate an array object of the ArrayList class, we can talk about some of the method objects!

The first one we will go over is the add method. We use it using the dot operator after the array name. I will add a few elements to this array as follows:

 lunch.add("fajitas");
 lunch.add("mangu");
 lunch.add("pizza");
 lunch.add("Mexican Wrap");
 lunch.add("cheeseburger");

We can easily print out the contents of this array by:

System.out.println(lunch); 

or retrieve an element of this array by using the get method;

 System.out.println(lunch.get(1));

If we wanted to remove an element from this array we would use the remove method with an index number

 lunch.remove(4);
In this case will remove the cheeseburger element

If we wanted to replace an element we will use the set method which takes two arguments the index of the element you will be replacing and the actual item you want to supplant it with:

    lunch.set(2, "salad");

In this case we replaced “pizza” with salad (a healthier alternative :))

I have also provided an example of combining two methods where we provide an index to verify if the element in that index in this case index 2 equals the argument we feed into the .equals method.

 if ( lunch.get(2).equals("pizza")){
    System.out.println(" We are having pizza today! :P");
    }

We can also retrieve the array size using the size method like this

System.out.println(lunch.size()); 

If we wanted to clear all the contents of an array we can use the clear method.

lunch.clear()

I hope you enjoyed learning the basics of using the ArrayList class! In the following link you can view my colleague’s blog where he goes on to give you examples of the ArrayList class objects in more intricate ways!

https://github.com/Nunez350/arrayList

Join the Conversation

  1. Unknown's avatar

1 Comment

Leave a comment

Design a site like this with WordPress.com
Get started