用Java比较可比较数组

| 我想比较一系列可比对象。最简单的方法如下所示(细节未显示):
public class ArrayComparable implements Comparable<ArrayComparable>{
ArrayList<Comparable<?>> list = new ArrayList<Comparable<?>>();

@Override
public int compareTo(ArrayComparable ac) {
    Iterator<Comparable<?>> itr = ac.list.iterator();
    for(Comparable<?> l : list) {
        Comparable<?> itrNext = itr.next();
        if(itrNext.getClass() == l.getClass()) {
            if(itrNext.compareTo(l)) {
                //something
            } else {
                //other stuff
            }
        } else {
            //some other thing
        }
    }
}
当然,这里的问题是,像ѭ2in一样的
compareTo
将不起作用,并给出错误:
The method compareTo(capture#6-of ?) in the type Comparable<capture#6-of ?> is not applicable for the arguments (Comparable<capture#7-of ?>)
我知道为什么(就该方法而言,我可能会将苹果与橙子进行比较)。另一方面,我知道我不是在比较事物之前先检查事物的类别。 那么有什么办法可以使我的工作呢?不用担心比较任何可比较数组的合理性,因为我有充分的理由要这么做。 编辑-所以我为什么要做这样的事情。假设我想拥有一系列可比对象,并且我不在乎每个索引中包含什么,只要类型对应即可,并且可以将它们进行比较。然后,我可以对这些数组进行一般的字典比较。这样,我不必为
(int,int)
(int, string)
,以及
(string, double, string)
或任何您需要的东西写一个可比的东西。我只写一个,只要确保类型匹配(并且我可以),我就很好。     
已邀请:
在您当前使用ѭ8type的任何地方使用原始类型
Comparable
都可以。实际上,如果需要,您可以将其放在一个地方:
if (((Comparable) itrNext).compareTo(l) == 0)
    
ArrayComparable
用作泛型类,以便您可以正确地对泛型进行参数化,而不是在所有地方都使用
<?>
。哦,您也可以实施
Iterable
public class ArrayComparable<T> implements Comparable<ArrayComparable<T>>, Iterable<T>
{
    List<Comparable<T>> list = new ArrayList<Comparable<T>>();

    @Override
    public int compareTo(ArrayComparable<T> ac)
    {
        // snip
    }

    @Override
    public Iterator<T> iterator()
    {
        return list.iterator();
    }
}
    
尝试这个:
    if(itrNext.getClass().cast(itrNext).compareTo(l.getClass().cast(l))) {
        //something
    } else {
        //other stuff
    }
    
public class GenericDemo<T>{
  T g;
 public <T extends Comparable<T>> void printData(T a[]){
    T max = a[0];
    if(a[1].compareTo(max)>0){
        max=a[1];
    }
    if(a[2].compareTo(max)>0){
    max=a[1];
    }
    System.out.println(max);
    System.out.println(\"DataType: \" +a.getClass().getName());

      }
    public static void main(String[] ar)
    {
     Integer a[]={1,2,3};
     Byte b[]= {4,6,7};
     Short c[]={6,8,9};

     GenericDemo g = new GenericDemo();
     g.printData(a);
     g.printData(b);
     g.printData(c);
     } 
}
    
一个好的答案是:
public final class ArrayComparable<T extends Comparable<T>>
    implements Comparable<ArrayComparable<T>> {

    private final ArrayList<T> list = new ArrayList<>();

    @Override
    public int compareTo(final ArrayComparable<T> other) {

        final Iterator<T> it = other.list.iterator();

        for (final T element : list) {
            final T otherElement = it.next();

            final int comparison = element.compareTo(otherElement);
            if (comparison < 0) {
                // something
            } else if (comparison > 0) {
                // other stuff
            } else {
                // other stuff
            }
        }
        return 0;
    }
}
    

要回复问题请先登录注册