Wednesday, 31 May 2017

Java Collections Tutorial 1: ArrayList



The ArrayList is perhaps the simplest and most lightweight class in Java’s collections framework. What an ArrayList is, fundamentally, is an internally managed array. In other words, it is an expandable array.

In the introduction to core Java tutorials, I introduced ArrayList as an example in generic classes. So if we were to declare an ArrayList that holds objects of type String, then we would declare it like this:

ArrayList<String> names = new ArrayList<String>();

If we create an ArrayList to hold doubles, here is how we do it: 

ArrayList<Double> prices = new ArrayList<Double>();

If we create an ArrayList to hold integers, here is how we do it:

ArrayList<Integer> numbers = new ArrayList<Integer>();

Notice that in all the above example, I use the wrapper types and not the primitive types. So instead of int, I use Integer. Instead of double, I use Double. Java will not allow you to use primitive types as parameters in generic classes. You always need to use the wrapper classes.

Adding Items to an ArrayList

When adding items to an ArrayList, we use the add() method. You can use the add() method in isolation or in a loop. It depends with what you want to add.
For example, if I want to add a couple of names to an ArrayList of type String, I would do the following:

ArrayList<String> names = new ArrayList<String>();
names.add(“Brian”);
names.add(“Veronicah”);
names.add(“Victor”);

If I want to add numbers from one to ten to an ArrayList holding integers, here is how I would do it using a for loop:

ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i  = 0; i <= 10; i++) {
            numbers.add(i);
}

Figure 1 Adding Items to an ArrayList


Removing Items from an ArrayList


To remove items from an ArrayList, we use the remove() method. We pass the index of the item we want to remove as a parameter to the method. Remember that in ArrayList, just like in arrays, we always start counting the items at index zero. So if we want to remove the third item, we call remove(2).

So let’s say we have the array of numbers that we created above. Here is how we would remove the 4th item, which is the number 4.

ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i  = 0; i =< 10; i++) {
            numbers.add(i);
}
numbers.remove(4); 


Figure 2: Removing items from an ArrayList


Iterating through an ArrayList
To iterate through an ArrayList, you can either use a for loop or a for each loop. So say we want to display all the values in the ArrayList above that contains integers. Here is how we would do it:

a) Using a for loop:

ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i  = 0; i =< 10; i++) {
            numbers.add(i);
}
numbers.remove(4);
for(int i  = 0; i < numbers.size(); i++) {
            System.out.println(numbers.get(i));
} 



Figure 3 Iterating through an ArrayList


b) Using a for each loop:
ArrayList<Integer> numbers = new ArrayList<Integer>();
for(int i  = 0; i =< 10; i++) {
            numbers.add(i);
}
numbers.remove(4);
for(Integer number: numbers) {
            System.out.println(number);
} 


Figure 4 Iterating through ArrayList using a foreach loop


Notice that we use the get() method to access the value stored at a certain index.
Caution About the Use of ArrayList
Here is how ArrayLists work: When you create a new ArrayList, you can specify its size in its constructor like this:

ArrayList<Integer> numbers = new ArrayList<Integer>(100);

So the above ArrayList will hold 100 integers. However, it’s not a must for you to specify the size of the ArrayList, as you can see from all the preceding examples in this tutorial. If you don’t supply the size for your ArrayList, Java will give it a default size of 10 and then if you exceed those values, it will copy the contents to a new ArrayList with a larger size. 

ArrayLists are very slow when you add or remove items from any other location, other than the end. If you are constantly adding to or removing items from the end of an ArrayList, then that’s okay. However, if you are adding or removing items from the beginning or middle or an ArrayList constantly, then your code will be very slow. If your program has an implementation that constantly removes items from the beginning or middle of an ArrayList, consider using a LinkedList, which we discuss in the next tutorial.

Practice working with ArrayLists of different types. Try adding items, iterating through them and removing them. Have some fun.

In the next tutorial, we are going to look at LinkedList.

As usual, if you have any questions or comments, please leave them in the comments section below and I will address them.

Until next time, stay safe. 

No comments:

Post a Comment