实现从同类产品扩展的通用接口

时间:2018-09-22 12:55:48

标签: java data-structures

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;
    }}

我想实现一个具有以下接口的通用数组类

仅创建并返回my对象的类MyArrayFactory 接口MyArray的实现。

这是我的实现类:

     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;



        }

    }

那么我应该对实现类做什么以解决处理泛型类型的问题?班级铸造等等 有没有一种特殊的方法来拥有特殊的compareTo方法?

1 个答案:

答案 0 :(得分:0)

如果我们看看TreeMap是如何做到的,它可能使我们知道如何实现它:

根据其性质(作为二叉树),TreeMap需要比较键,以便将它们正确地放入二叉树中,但是TreeMap类需要具有以下通用类型的键: 不是必须实施Compareable。这是声明:

public class TreeMap<K,V> extends ...

TreeMap构造函数可以没有参数,也可以没有Comparator参数:

public TreeMap() {
    comparator = null;
}

public TreeMap(Comparator<? super K> comparator) {
    this.comparator = comparator;
}

它具有内部使用的compare函数:

final int compare(Object k1, Object k2) {
    return comparator == null ? ((Comparable<? super K>)k1).compareTo((K)k2)
        : comparator.compare((K)k1, (K)k2);
}

compare函数解释其工作原理:首先尝试使用提供的comparator对象比较两个对象。如果不可用,则假定对象是可比较的,并尝试通过使用其Comparable实现并将它们转换为Comparable实例来比较它们。如果实际上他们没有实现Comparable,则会抛出ClassCastException,在这种情况下TreeMap无法正常工作。

因此,结论是,有一种方法可以通过提供外部或内部比较函数来实现可以比较任何两个对象的集合,但是泛型必须具有较小的限制,如果使用不正确,则会发生错误。