Author:
Daisy Williams
Oct
7
A for-each loop is an enhanced for loop. It is used to iterate over arrays and collections. This feature was added to Java to make the access and retrieval of elements of arrays and collections faster. The syntax of a for-each loop is as follows:
for(type itrvar: collection)
{
}
where, itrvar is the variable that iterates through the elements of the collection.
Permanent link to this post (64 words, estimated 15 secs reading time)
Author:
Daisy Williams
Sep
25
The Comparable interface is used to sort collections and arrays of objects using the Collections.sort() and java.utils.Arrays.sort() methods respectively. The objects of the class implementing the Comparable interface can be ordered.
The Comparable interface in the generic form is written as follows:
interface Comparable, where T is the name of the type parameter.
All classes implementing the Comparable interface must implement the compareTo() method that has the return type as an integer. The signature of the compareTo() method is as follows
int i = object1.compareTo(object2)
- If object1 < object2: The value of i returned will be negative.