← Home

ArrayList

So far you have met arrays, but now it's time to meet ArrayLists. An ArrayList is almost identical to an array with one or tow major exceptions. Firstly, an ArrayList is resizeable. An array, on the other hand, has a set size which cannot be exceeded. Secondly, an ArrayList is a lot easier to use. There are a set list of methods which do various things, which you will meet later. The use them you will have to import this:

1java.util.ArrayList

How to create an ArrayList

An ArrayList is declared and instantiated in exactly the same way as you do for every other component. You do not need to declare the size of these ArrayLists (as their sizes are variable), and can declare them in the following ways.

1ArrayList al1 = new ArrayList();
2ArrayList al2 = new ArrayList(5);
3ArrayList al3 = new ArrayList(initialCapacity);

The first way assumes that the length is going to be 10, because you have not stated otherwise. The second one however does declare a length, which is 5. The third one also has the same effect, setting the length to whatever initialCapacity turns out to be. You may want to also declare what kind of ArrayList you want i.e. what data type you want the data to be. To do this you will need to instead declare this:

1ArrayList<String> al1 = new ArrayList<String>();
2ArrayList<Integer> al2 = new ArrayList<Integer>();

As expected, the first one creates an ArrayList with Strings, and the second one contains Integers. You may also notice that the data type is a class type rather than a primitive type i.e. we have used Integer insted of int. This is the same for boolean and char.

A Few Useful Methods

1) Adding an element:

1al.add(e) //Adds an element at the end, where 'al' is the name of an ArrayList

As mentioned in the comment, whatever e turns out to be (a number, a String, a char etc) will be added to the end of the ArrayList. It is worth mentioning that e must be the same data type as the ArrayList itself, as in you cant add an int to a String ArrayList.

2) Removing all the data:

1al.clear(); //'al' is the name of an ArrayList

Have a guess what that does.

3) Finding the size of the ArrayList:

1al.size();

You wouldn't just use that on its own however, you would need to set a variable to be that value or print that value out.

4) Removing single elements:

1al.remove("F");
2al.remove(2);

The first command above removes the letter F from the ArrayList (bearing in mind that the letter F is in the ArrayList). The second one removes the value at index 2.

5) Finding the contents of the ArrayList:

1System.out.println(al);

This will print out the contents of the ArrayList. This is not the only thing you can do with the contents, for example you could set a variable to be it.

So, using the above concepts you could create a chain of manipulation of an ArrayList, by means of example:

1ArrayList<String> al = new ArrayList<String>();
2System.out.println("The size of the list: " + al.size());
3
4al.add("A");
5al.add("B");
6al.add("C");
7al.add("D");
8System.out.println("New size: " + al.size());
9
10System.out.println("The contents of the list: " + al);
11
12al.add(1, "100000000000");
13System.out.println("The new contents of the list: " + al);
14
15al.remove("D");
16al.remove(2);
17System.out.println("The contents after deletion: " + al);
18
19al.clear();
20System.out.println("The size after complete deletion: " + al.size());

This would print out:

1The size of the list: 0
2New size: 4
3The contents of the list: [A, B, C, D]
4The new contents of the list: [A, 100000000000, C, D]
5The contents after deletion: [A, 100000000000]
6The size after complete deletion: 0

Now for a thing I believe is useless, but that is not a reason to skip it →