perhaps this exaplanation will help:
http://stackoverflow.com/questions/2231804/java-interface-extends-comparable
public static <AnyType> AnyType findMax( AnyType[] arr)
{
int maxIndex = 0;
for(int i=1; i<arr.length; i++)
if(arr[i].compareTo(arr[maxIndex]) > 0)
maxIndex = i;
return arr[maxIndex];
}
public static <T extends Comparable<T>> T findMax( T[] arr)
{
int maxIndex = 0;
for(int i=1; i<arr.length; i++)
if(arr[i].compareTo(arr[maxIndex]) > 0)
maxIndex = i;
return arr[maxIndex];
}
public static <AnyType extends Comparable<AnyType>> AnyType findMax( AnyType[] arr)
{
int maxIndex = 0;
for(int i=1; i<arr.length; i++)
if(arr[i].compareTo(arr[maxIndex]) > 0)
maxIndex = i;
return arr[maxIndex];
}
public static <AnyType extends Comparable> AnyType findMax( AnyType[] arr)
{
int maxIndex = 0;
for(int i=1; i<arr.length; i++)
if(arr[i].compareTo(arr[maxIndex]) > 0)
maxIndex = i;
return arr[maxIndex];
}
public class Box<T> {
private T t; // T stands for "Type"
public void setEntity(T t) {
this.t = t;
}
public T getEntity() {
return t;
}
}
arr[i].compareTo(arr[maxIndex])
is defined because element of array arr is isntance of T, therefore compiler needs just this way <T extends Comparable<T>> and thisIf you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
Image decoding from Camera | 3 | 73 | |
Java Server Faces parameter pass? | 6 | 39 | |
javap bin | 2 | 30 | |
object oriented programming comparison | 5 | 57 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
23 Experts available now in Live!