Getting to know Arraylist in Java


Since some of my classmates are struggling with Arraylist in their assignment. I would like to make some lines of word about this new savior for those who are tired of using the normal array in Java.

What is good about Arraylist?

It is not fixed size, which is so great when you don’t have to scratch your head to finger out how many elements that you should initial your array in Java.
With Arraylist, you can add as much element to it as you want.

Declare and define an Arraylist.

Suppose that you want to create an Arraylist of String:

public ArrayList<String> strings = new ArrayList<>();

where you should put Datatype inside <>;
Now you want to make an array of a Class of Doctor, which is a reference type, this is how you declare the ArrayList:

public ArrayList<Doctor> doctors;

and then you assign it to a new arraylist;

doctors = new ArrayList<>();

or in one line

public ArrayList<Doctor> doctors = new ArrayList<>();

Don’t forget to import the arraylist:

import java.util.ArrayList;

How ArrayList works with primitive datatype?

Arraylist basically doesn’t work with the primitive datatype, so that you can’t do something like this:

private ArrayList<int> ints;

To deal with primitive, you have to use what’s called wrapper.

private ArrayList<Integer> ints;

For each primitive, we have the respective wrapper. You can refer to here for all the wrapper:

To add an element to ArrayList.

It’s super easy to add an element to ArrayList when you look back on the normal array. You don’t have to deal with the index to add in ArrayList. All that you need to do is you the built-in method .add().

doctors.add(new Doctor());

That’s it.

To loop through arraylist:

You still you for loop for loop through the the array, but you can’t use .length here since it not a normal array. To get the size of the array, we use .size().

for (int i =0, i < doctors.size(), i++){
//your code here

Index in arraylist.

doctors[0] won’t work in this case as we’re using arraylist. To get the value of specific index we use .get(). This method needs one parameter which is an index with int type.

for (int i =0, i < doctors.size(), i++){

Remove an element in arraylist

When using normal array we can assign value of null the the specific element. With arraylist, we have a method that the name show it function .remove(). Same with get, we need to pass in the index as parameter.

for (int i =0, i < doctors.size(), i++){

Compare in arraylist

Since using .get(index) in arraylist will return a value of that index, which could be a specific object that you added before. You can access or call any method right after it. Suppose we in the Class Doctor, we have the field of name and age, getter methods of getName() and getAge(), now you want to compare the name and age with the ones outside

for (int i =0, i < doctors.size(), i++){
 if (doctors.get(i).getName().equals(“Akram”) && 
                   doctors.get(i).getAge() == 18 ) 
    System.out.println(“Go to eat");

More is to be added…


Please enter your comment!
Please enter your name here