Using Lists in Java

Lists are the most often used data structures in Java programming. Having looked at the three concrete implementations in the last article we look at how to use List in real life. We analyze the most important methods in List interface and present a sample program to demonstrate usage of ArrayList.

Important Methods in List

To effectively declare and use a List, you need to a few important methods and constructors.

Creating a List

A List is typically created by using a new operator on a concrete List class using default (argument less) constructor and assigning the resulting reference to a variable of type List .

You can also specify the initial capacity of your List in the constructor. But this is not the usual practice.

For example, if you have a list of continents you know that your list will be always less than 7 in size you can define it as follows –

Adding Elements to List

The add() method defined in Collection interface is used to add elements to the List.

By default the elements are added to the end of List. add method has an overloaded variant that allows you to specify the index number where to put an element.

Retrieving Elements from a List

The counterpart of the add() method is the get() method which allows you to get an element at a particular index.

Though you can keep incrementing the index numbers and use get method to traverse a List, itis not the preferred method. The preferred way of traversing a List (and any Collection for that matter) is to use Iterator obtained from iterator() factory method. The hasNext() method of the iterator is used to access all the elements one by one –

Sometimes, as in the example above, you want to read all elements of a List one by one. There is a simpler, cleaner construct for this task. The for-each loop, introduced in Java 5, is effectively a shorthand for the above way of fetching an iterator and using next() on the iterator.

Removing Elements from a List

The remove() method defined in the Collection interface is used to, well, remove elements. Once an element is removed, all subsequent elements are moved up the List and thus their index decreased by one.

There are two overloaded variants of remove –

  1. remove(int index) – removes the element at number index
  2. remove(Object obj) – removes the first occurrence of the object obj. Nothing happens if obj is not found.

Clearing a List

The List interface provides an additional method to delete all the elements at one go. This clear() method effectively discards the old list and allows you to begin with clean slate.

Checking the size of a List

Lists are dynamic data structures, so you will usually not bother about their size. But if you want, you can check it using the intuitively named size() method.

Complete Code Listing

The source code below demonstrates the concepts discussed above

The output of the program is –

Leave a comment

Your email address will not be published. Required fields are marked *