public interface MyArray <T extends Comparable <T>> {
T get( int i);
void set ( int i, T e);
int min ();
int max ();
int nbBetween (T e1 , T e2);
}
public class MyArrayFactory {
public static <T extends Comparable <T>> MyArray <T> getMyArray( int n) {
MyArray<T> ar;
ar = new ArrayImplentation(n);
return ar;
}}
I want to implement a generic array class having the following interface
the class MyArrayFactory which simply creates and returns an object of my implementation of the interface MyArray.
and this is my implementation class:
public class ArrayImplentation<T extends Comparable <T>> implements MyArray<T>{
public MyArray[]arr;
public ArrayImplentation(int n) {
arr= (MyArray[])new Object[n];
}
// Return the element at position i
public T get( int i) {
T t = (T)arr[i];
return t;
}
// Set the element at position i
public void set ( int i, T e) {
arr[i]=(MyArray)e;
}
// Return the index of smallest element in the array ( index of first occurrence returned )
public int min () {
T minValue = (T)arr[0];
for(int i = 1; i < arr.length; i++) {
if(minValue.compareTo((T)arr[i])>0) {
return i;
}}
return 0;}
// Return the index of largest element in the array ( index of first occurrence returned )
public int max () {
T maxValue = (T)arr[0];
for(int i = 1; i < arr.length; i++) {
if(maxValue.compareTo((T)arr[i])<0) {
return i;
}}
return 0;}
// Return the number of elements largest or equal e1 and smallest or equal e2
public int nbBetween (T e1 , T e2) {
int index=0;
int index2=0;
int count=0;
for(int i = 0; i < arr.length; i++)
if(e1.compareTo((T)arr[i])==0)
index=i;
for(int i = 0; i < arr.length; i++)
if(e2.compareTo((T)arr[i])==0)
index2=i;
for(int i=index;i<index2;i++)
count++;
return count;
}
}
so what should I do with implementation class to solve the problem with dealing with generic types ? class casting and a lot more and is there a specific way to have a special compareTo method ?
If we look at how TreeMap
does this, it may give us an idea how we can implement it:
By it's nature (of being a binary tree), TreeMap
needs to compare keys, so that they are put correctly into a binary tree, yet the TreeMap
class requires a key of generic type that does not have to implement Compareable
. This is the declaration:
public class TreeMap<K,V> extends ...
The TreeMap
constructor can have no arguments, or a Comparator
argument:
public TreeMap() {
comparator = null;
}
public TreeMap(Comparator<? super K> comparator) {
this.comparator = comparator;
}
And it has a compare
function it uses internally:
final int compare(Object k1, Object k2) {
return comparator == null ? ((Comparable<? super K>)k1).compareTo((K)k2)
: comparator.compare((K)k1, (K)k2);
}
This compare
function explains how it works: It first tries to use the provided comparator
object to compare two objects. If it isn't available, it assumes that the objects are comparable, and tries to compare them by using their Comparable
implementation, by casting them into Comparable
instances. If they're, in fact, not implementing Comparable
, a ClassCastException
is thrown, in which case the TreeMap
doesn't work correctly.
So, the conclusion is that there is a way to implement a collection that can compare any two objects by providing either external or internal comparing functions, but then the generics have to have lesser restrictions, and errors can occur if not used correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.