【问题标题】:Unchecked cast problem未经检查的演员表问题
【发布时间】:2010-11-07 02:08:19
【问题描述】:

嘿,我正在尝试实现 ShellSort 算法,但现在我遇到了一个问题:

warning: [unchecked] unchecked cast
found : java.util.Vector
required: java.util.Vector<java.lang.Object>
Vector<Object> vcur = (Vector<Object>)currentCols[tcur];
vtmp 相同。

我不知道问题出在哪里。如果你能帮助我,那就太好了。 :)
这是我的代码:

public static Vector<Object> shellSort(Vector<Object> ul) {
    int lcount = ul.size();
    int colcount = 4; // 2^x
    Vector[] currentCols = { ul };
    for(; colcount > 0; colcount = (colcount / 2)) {
        Vector[] tmpCols = new Vector[colcount];
        for(int t1 = 0; t1 < colcount; t1++) {
            tmpCols[t1] = new Vector<Object>();
        }
        int tcur = 0;
        int tcurlvl = 0;
        int ttmp = 0;
        for(int t2 = 0; t2 < lcount; t2++) {
            Vector<Object> vcur = (Vector<Object>)currentCols[tcur];
            Vector<Object> vtmp = (Vector<Object>)tmpCols[ttmp];
            vtmp.addElement((Object)vcur.elementAt(tcurlvl));

            // step to next place
            tcur++;
            ttmp++;
            if(tcur == currentCols.length) { tcur = 0; tcurlvl++; }
            if(ttmp == tmpCols.length) { ttmp = 0; }
        }
    }
    return ul;
}

【问题讨论】:

    标签: java casting


    【解决方案1】:

    我相信这里的问题是类型擦除。在运行时,Vector 就是 Vector。例如:

    Vector<String> stuff = new Vector<String>()
    Vector<Object> objects = (Vector<Object>)stuff
    

    它会编译,但在运行时当您尝试将 Int 放入对象时会失败。

    【讨论】:

    • Java 不允许你那样投射,上次我检查过——这正是你给出的原因。
    • 显然是让他这样施法,却给了他未经检查的施法警告?
    • 警告更有可能与代码中的Vector[] 声明有关。 Vector[]Vector&lt;Object&gt;[] 在字节码中可能看起来很相似,但它们对于编译器来说并不完全相同。
    【解决方案2】:

    这仍然不能解决问题(您的代码不起作用......永远不会排序)但它已更新为使用 List 而不是 Vector(Vector 已过时,不能被弃用,因为官方 API 依赖于它)。除非您需要 Vector 内置的同步功能,否则请改用 List/ArrayList。

    我还将数组创建更改为更接近泛型(不能真正进行泛型数组创建)。

    最后我把整个方法变成了通用的,这样它就可以在类型安全的情况下处理 Object 以外的东西(比如 List 或 List 等)。

    它仍然会发出警告。也许您应该在担心这个特定警告之前让代码正常工作(通常我不建议这样做,但使用泛型有时可能是更好的方法)。

    public static <T> List<T> shellSort(List<T> ul) 
    {
        int lcount = ul.size();
        int colcount = 4; // 2^x
    
        List<T>[] currentCols = (List<T>[])Array.newInstance(List.class, 1);
        currentCols[0] = ul;
    
        for(; colcount > 0; colcount = (colcount / 2))
        {
            List<T>[] tmpCols = (List<T>[])Array.newInstance(List.class, colcount);
    
            for(int t1 = 0; t1 < colcount; t1++)
            {
                tmpCols[t1] = new ArrayList<T>();
            }
    
            int tcur = 0;
            int tcurlvl = 0;
            int ttmp = 0;
    
            for(int t2 = 0; t2 < lcount; t2++)
            {
                List<T> vcur = currentCols[tcur];
                List<T> vtmp = tmpCols[ttmp];
                vtmp.add(vcur.get(tcurlvl));
    
                // step to next place
                tcur++;
                ttmp++;
                if(tcur == currentCols.length) { tcur = 0; tcurlvl++; }
                if(ttmp == tmpCols.length) { ttmp = 0; }
            }
        }
    
        return ul;
    }
    

    【讨论】:

    • 在尝试了您的代码并对其进行了一些修复之后,它又出现了:警告:[unchecked] unchecked cast found : java.lang.Object required: java.util.List[] List[] tmpCols = (List[])Array.newInstance(List.class, colcount);
    • 是的。没有办法消除投诉(据我所知),但此代码更接近您所追求的(即使您只使用 Array.newInstance 代码)。一般来说,编译器警告是一件坏事,在这种情况下,由于没有办法绕过它,你只需要接受它。
    • 您对 Array.newInstance 的使用不正确。您正在创建一个“列表”数组。即使是一个简单的 new Object[size];会更好
    【解决方案3】:

    你为什么声明currentCols

    Vector[] currentCols = { ul };
    

    此数组始终包含一个元素 - ul。只需将currentCols[..] 替换为ul,错误也会消失。

    【讨论】:

      猜你喜欢
      • 2017-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      相关资源
      最近更新 更多