Consider this tutorial as an introduction to generic classes,
which we will look into deeply in the tutorials under Java Collections.
When we refer to something as generic, we mean that it can
assume many shapes or work with many different forms. For the purpose of this
introductory tutorial, I will use ArrayList. There are several other generic
classes, but we will deal with those in the Collections tutorials.
I will first start by telling you how generic classes were
used in the past, i.e. in Java 5. Then, I will show you how we use ArrayLists
today. This difference will help you understand Java deeper.
Generic Classes in Java 5
In Java 5, this is how we used ArrayList and any other class
in the collections framework for that matter.
We declare an ArrayList like this:
ArrayList flowers = new ArrayList();
Then we add items into the ArrayList like this:
flowers.add(“rose”);
flowers.add(“dandelion”);
flowers.add(“lilies”);
If we want to remove an item from the ArrayList, we can
either refer to it by its index. Remember that just like arrays, the indices of
objects in an ArrayList start from zero. So, if I want to remove the item
“dandelion”, I will write the following:
flowers.remove(1);
Figure 1: Generic classes in Java 5.
Notice that in order to output the actual strings of the
flowers in the ArrayList, I use the toString() method. This is because Java
will return an Object. So, we have to tell Java that we want the output to be,
not of type Object, but of type String.
Generic
Classes in Java 6 and Above
After Java 5, generic classes worked a bit differently.
For example, when creating the generic class, you now could
specify the datatype of objects it holds using angle brackets. For example, the
modern way to declare the above ArrayList of flowers would be like this:
ArrayList<String> flowers = new
ArrayList<String>();
Now, we add items to the ArrayList in much the same way as we
did in Java 5, using the add() function like this:
flowers.add(“roses”);
flowers.add(“dandelion”);
flowers.add(“lilies”);
We also remove items from the ArrayList in the same way we
did in Java 5, by referring to the object using its index in the remove()
method like this:
flowers.remove(2);
The above statement would remove “lilies” from the ArrayList
because the indices of the items in the ArrayList start at zero.
The importance of using the angle brackets as shown above
becomes clear when we want to output the items or use them for any other
operation. Consider outputting the items in the ArrayList onto the console.
Here is how we would do it using a foreach loop:
for (String flower : flowers) {
System.out.println(flower);
}
Figure 2: Generic classes in Java 6 and above.
In the next tutorial, I am going to teach you about the
wildcard operator in generic classes.
I hope you have enjoyed reading this tutorial. It is one of
the shorter ones. If you have any questions or comments, please make sure to
post them in the comments section below and I will address them as soon as
possible.
Until next time, take care.


No comments:
Post a Comment