比较 Java 中的可比较数组

发布于 2024-11-03 02:13:10 字数 1296 浏览 1 评论 0原文

我想比较一系列可比较的东西。最简单的方法如下(详细信息未显示):

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

当然,这里的问题是 itrNext.compareTo(l) 中的 compareTo 将无法工作并给出错误: Comparable类型中的方法compareTo(capture#6-of ?)不适用于参数 (Comparable) 我明白为什么(就方法而言,我可能会将苹果与橙子进行比较)。另一方面,我知道我不是,因为我在比较事物之前先检查它们的类别。

那么有什么办法可以让我完成这项工作吗?不要担心比较任何可比较数组的合理性,因为我有充分的理由这样做。

编辑-那么我为什么要做这样的事情。假设我想要一个可比较的数组,并且我不关心每个索引中包含什么,只要类型对应,并且可以比较它们即可。然后我可以在这些数组之间进行一般的字典顺序比较。这样我就不必为 (int,int)(int, string) 以及 (string, double, string)< 编写比较/code> 或任何你需要的东西。我只写一个,只要我确保类型匹配(并且我可以),我就可以开始了。

I want to compare an array of comparables. The simplest way seems the following (details not shown):

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

Of course the problem here is that the compareTo as in itrNext.compareTo(l) will not work giving the error: The method compareTo(capture#6-of ?) in the type Comparable<capture#6-of ?> is not applicable for the arguments (Comparable<capture#7-of ?>)
which I understand why (as far as the method is concerned I might be comparing apples to oranges). On the other hand, I know I am not as I check for the class of things before comparing them.

So is there a way I can make this work? Don't worry about the sanity of comparing arrays of any comparables, as I have a good reason why I want to do that.

EDIT- SO why would I want to do something like this. Say I wanted to have an array of comparables, and I didn't care what was contained in each index, as long as the types corresponded, and they could be compared. Then I could do a general lexicographical compare between these arrays. This way I don't have to write a comparable for (int,int) and (int, string), and (string, double, string) or whatever you need. I just write one, and as long as I make sure that the types match (and I can), I am good to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

雨夜星沙 2024-11-10 02:13:10

在当前使用 Comparable 的地方,使用原始类型 Comparable 应该可以。实际上,如果您愿意,您可以在一个地方执行此操作:

if (((Comparable) itrNext).compareTo(l) == 0)

Using the raw type Comparable wherever you're currently using Comparable<?> should work. Actually, you could just do that in one place if you want:

if (((Comparable) itrNext).compareTo(l) == 0)
枕梦 2024-11-10 02:13:10

将 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();
    }
}

Make ArrayComparable a generic class so that you can properly parameterize the generics rather than using <?> everywhere. Oh, and you might as well implement Iterable as well.

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();
    }
}
白色秋天 2024-11-10 02:13:10

试试这个:

    if(itrNext.getClass().cast(itrNext).compareTo(l.getClass().cast(l))) {
        //something
    } else {
        //other stuff
    }

Try this:

    if(itrNext.getClass().cast(itrNext).compareTo(l.getClass().cast(l))) {
        //something
    } else {
        //other stuff
    }
蓝眼睛不忧郁 2024-11-10 02:13:10
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 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);
     } 
}
尝蛊 2024-11-10 02:13:10

对此的一个很好的答案是:

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

A good answer to this would be:

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文