PeopleSoft Adoption Made Smooth & Simple!
On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool. Claim Your Free WalkMe Account Now
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 |
---|---|---|---|
check java version using powershell | 13 | 306 | |
java mysql insert application | 14 | 47 | |
What is the use of Forwarding Class in java | 1 | 37 | |
Java array | 10 | 65 |
Join the community of 500,000 technology professionals and ask your questions.