Basic operations: in List Part II

Adding elements
The methods add(Object), add(index, Object) and addAll() are used to add elements to the list. It requires to add elements of the same type (or sub type) as the type parameter declared by the list. For example:

List<String> listStrings = new ArrayList<>();
 
// OK to add Strings:
listStrings.add("One");
listStrings.add("Two");
listStrings.add("Three");
 
// But this will cause compile error
listStrings.add(123);

 

Adding elements of sub types of the declared type:

List<Number> linkedNumbers = new LinkedList<>();
 
linkedNumbers.add(new Integer(123));
linkedNumbers.add(new Float(3.1415));
linkedNumbers.add(new Double(299.988));
linkedNumbers.add(new Long(67000));

 

We can insert an element into the list at a specified index, for example:

listStrings.add(1, “Four”);
That inserts the String “Four” at the 2nd position in the list.
We can also add all elements of an existing collection to the end of the list:

listStrings.addAll(listWords);
Or add the elements to the list at a specified position:

listStrings.addAll(2, listWords);
That inserts all elements of the listWords collection at 3rd position of the listStrings collection.

 

 

Retrieving elements
The get() method is used to retrieve an element from the list at a specified index. For example, the following code gets an element at 2nd position in the array list and an element at 4th position in the linked list:

String element = listStrings.get(1);
Number number = linkedNumbers.get(3);
For a LinkedList implementation, we can get the first and the last elements like this:

LinkedList<Number> numbers = new LinkedList<Number>();
// add elements to the list…

// get the first and the last elements:
Number first = numbers.getFirst();
Number last = numbers.getLast();
Note that the getFirst() and getLast() methods are specific to the LinkedList class.

Updating elements
Use the set(index, element) method to replace the element at the specified index by the specified element. For example:

listStrings.set(2, “Hi”);
That replaces the 3rd element in the list by the new String “Hi”.

Removing elements
To remove an element from the list, use the remove(index) or remove(Object) method which removes the element at the specified index or by object reference. For example:
Remove the element at the 3rd position in the list:
listStrings.remove(2);
If the specified index is out of range (index < 0 or index >= list size), a java.lang.IndexOutOfBoundsException is thrown.
Remove the String element “Two” in the list:
listStrings.remove(“Two”);
Notes about the remove(Object) method:
It compares the specified object with the elements in the list using their equals() method, so if you use your own defined object type, make sure it implements the equals() method correctly.
It only removes the first occurrence of the specified element in the list (i.e. if a list contains duplicate elements, only the first element is removed).
It returns true if the list contained the specified element, or falseotherwise. Thus it’s recommended to check return value of this method, for example:
if (listStrings.remove(“Ten”)) {
System.out.println(“Removed”);
} else {
System.out.println(“There is no such element”);
}

To remove all elements in the list, use the clear() method:
listStrings.clear();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: