Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.
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 |
---|---|---|---|
groupSum5 challenge | 5 | 84 | |
JList custom Cell Renderer refresh | 15 | 56 | |
what is a "java.lang.System Property" ? | 20 | 65 | |
difference of if loops | 23 | 49 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
13 Experts available now in Live!