将类型Object转换为泛型

时间:2015-05-07 06:16:38

标签: java generics casting quicksort

我知道这是一个非常草率的快速入口,但它是我需要使用的那个。

我的问题是如何更改y数组的类型以在E行上输入x[k+i]=(E) (y[k])。我目前正在做的方式不会将对象转换为类型E。 quicksort工作正常(至少在递归之前),它只是此时的转换。

public class quicksort {
    static <E extends Comparable<E>> E[] quickSort(E[] x, int i, int j) {
        int pivot = 0;
        Object[] y = new Object[j - i + 1];
        for (int k = 0; k < y.length; k++) y[k] = x[k + i];
        int lput = i;
        int rput = j;
        for (int k = i + 1; k <= j; k++) {
            if (x[k].compareTo(x[pivot]) < 0) {
                y[(lput++) - i] = x[k];
            } else {
                y[(rput--) - i] = x[k];
            }
        }

        pivot = lput;
        y[lput - i] = pivot;
        for (int k = 0; k < y.length; k++)
            x[k + i] = (E) (y[k]); // this line
        quickSort(x, i, pivot - 1);
        quickSort(x, pivot + 1, j);
        System.out.println("got here");
        return x;
    }
}

这是堆栈跟踪。该行引用的是x[k + i] = (E) (y[k]);

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
Type mismatch: cannot convert from Object to E

at quicksort.quickSort(quicksort.java:23)
at ArrayListGui$6.actionPerformed(ArrayListGui.java:158)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

你所拥有的是最正确的方法。它编译并正常工作。

另一种方法是将数组创建为:

std::vector<Point2f&> center(contours.size() );
std::vector<float&> radius(contours.size() );

for( int i = 0; i < contours.size(); i++ )
{
  cv::approxPolyDP(Mat(contours[i]),Mat(contours_poly[i]), 3, true );
  boundRect[i] = boundingRect( Mat(contours_poly[i]) );
  minEnclosingCircle(Mat(contours_poly[i]),center[i],radius[i]);
}

然后当你把它拿出来时,你就不必投了它:

E[] y = (E[])new Comparable[j - i + 1];

但是,这有点危险,因为指向x[k + i] = (E) (y[k]); 的对象的实际运行时类是y不是 Comparable[],如果你不小心将E[]公开到方法的外部作为类型y,它可能导致其他代码段崩溃,这是意外的。在此方法中,您不会将E[]暴露给方法的外部,因此在这种情况下不会导致任何问题。

答案 1 :(得分:-2)

您可以通过传递type token来执行此操作,然后可以使用JDK的Array.newInstance(Class<?> componentType, int length)工厂方法创建类型的实例:

#include<stdio.h>
int main() {
        char* string="Connected to a:b:c:d completed (auth) id=3";
        char* fmt = "Connected to %s completed id=%d";
        char b[60]={0};
        int id = -1;

        sscanf(string, fmt, b, &id);

        printf("Auth :: %s :: id :: %d\n", b, id);
}