简体   繁体   English

转换ArrayList <Comparable> 变成另一个ArrayList类型

[英]Casting ArrayList<Comparable> into a different ArrayList type

I have a sorting method that takes an ArrayList<Comparable>, sorts it using the compateTo() method, then returns a sorted ArrayList<Comparable>. 我有一个排序方法,该方法采用ArrayList <Comparable>,使用compateTo()方法对其进行排序,然后返回已排序的ArrayList <Comparable>。 Here it is: 这里是:

public static ArrayList<Comparable> insertionSort(ArrayList<Comparable>
                                                          input) {
    Comparable temp;
    ArrayList<Comparable> result;

    result = (ArrayList<Comparable>) input.clone();

    if (result.size() > 1) {
        for (int k = 1; k < result.size(); k++) {
            for (int j = 1; j <= k; j++) {
                if (result.get(k - j).compareTo(result.get(k - j + 1)) >0){
                    temp = result.get(k - j + 1);
                    result.set(k - j + 1, result.get(k - j));
                    result.set(k - j, temp);
                }
            }
        }
    }

    return result;
}

Somewhere else in my program, I define DVD objects that implement the Comparable interface, create a bunch of them, and store them in an ArrayList<DVD> called members. 在程序的其他地方,我定义了实现Comparable接口的DVD对象,创建了一堆对象,并将它们存储在称为成员的ArrayList <DVD>中。 Now, when I try to sort members like this: 现在,当我尝试像这样对成员进行排序时:

members = (ArrayList<DVD>) YaSort.insertionSort(members);

I get the following error: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Comparable; 我收到以下错误:线程“主”中的异常java.lang.ClassCastException:[Ljava.lang.Comparable; cannot be cast to [LDVD; 无法转换为[LDVD;

How do I solve this? 我该如何解决? Thanks for your time. 谢谢你的时间。

The point of generics is that you shouldn't need to cast anything reference types. 泛型的要点是,您不需要强制转换任何引用类型。 Also, rare types, mixing generic and raw types, is bad. 同样,混合通用和原始类型的稀有类型也是不好的。 And generally List is preferred to ArrayList , by convention more than anything else. 通常,按惯例, ListArrayList更受青睐。

The start of your method should look something like: 方法的开始应类似于:

public static <T extends Comparable<? super T>> List<T> insertionSort(
    List<T> input
) {
    List<T> result  = new ArrayList<T>(input);

(Actually for maximum performance, probably not from an insertion sort over much data, doing something hacky with arrays is better. BTW: The error you are quoting appears to be from the use of arrays rather the collections.) (实际上,为了获得最佳性能,可能不是通过对大量数据进行插入排序,对数组进行一些改动比较好。BTW:您引用的错误似乎是由于使用数组而不是集合引起的。)

It seems that you want to sort a list of objects that implement Comparable and return a list of the same objects, not just Comparable . 似乎您想对实现Comparable的对象列表进行排序,并返回相同对象的列表,而不仅仅是Comparable In this case you can use method parameter that can be used by a compiler to determine the type of return result: 在这种情况下,您可以使用可由编译器用来确定返回结果类型的方法参数:

public static <T extends Comparable<? super T>> ArrayList<T>
    insertionSort(ArrayList<T> input) {
    ....
}

Then you can assign to members without cast: 然后,您可以不带演员就分配给members

members = YaSort.insertionSort(members);

You need to define members as ArrayList<Comparable> , because even thought DVD implements/extends Comparable ArrayList<DVD> does not implement/extend ArrayList<Comparable> - this is called covariance/contravariance and is not supported in Java. 您需要将members定义为ArrayList<Comparable> ,因为甚至考虑DVD实现/扩展Comparable ArrayList<DVD>都不实现/扩展ArrayList<Comparable> -这被称为协方差/逆方差,并且在Java中不受支持。 (It is actually supported in C#) (实际上在C#中受支持)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM