在Java 5中使用原始数组作为实际类型参数有什么问题?

前端之家收集整理的这篇文章主要介绍了在Java 5中使用原始数组作为实际类型参数有什么问题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用JDK1.5.0_u22来编译如下代码.我得到两个编译错误(进一步下面).它适用于JDK1.6.0u30,但我的项目仅限于 Java 5.如果我使用包装器类Float来补充float,它也可以正常工作.有人知道这里发生了什么吗?在一般情况下使用原始数组类型的简单错误或一般限制?
import java.util.*;
public class A {
   public static void main(String[] args) {
     List<float[]> list = Arrays.asList(new float[1],new float[3]);
     float[] key = new float[2];
     int index = Collections.binarySearch(list,key,new Comparator<float[]>() {
        public int compare(float[] f1,float[] f2) {
           return f1.length - f2.length;
        }
     });
     System.out.println(index);
   }
}

编译错误

C:\Users\mravn\Desktop>"c:\Program Files\Java\jdk1.5.0_22"\bin\javac A.java
A.java:4: incompatible types
found   : java.util.List<<nulltype>[]>
required: java.util.List<float[]>
       List<float[]> list = Arrays.asList(new float[1],new float[3]);
                                       ^
A.java:6: cannot find symbol
symbol  : method binarySearch(java.util.List<float[]>,float[],<anonymous java.util.Comparator<float[]>>)
location: class java.util.Collections
     int index = Collections.binarySearch(list,new Comparator<float[]>() {
                            ^
2 errors

C:\Users\mravn\Desktop>

解决方法

我将给出第一个编译器错误的答案,并继续挖掘,看看是否可以找到第二个编译器错误

运行以下内容本身不会使用jdk1.5.0_22进行错误

System.out.println(Arrays.asList(new float[1]).size());

自己运行…

System.out.println(Arrays.asList(new float[1],new float[3]).size());

产生这个讨厌的编译器错误

An exception has occurred in the compiler (1.5.0_22). Please file a bug at the Java Developer Connection (http://java.sun.com/webapps/bugreport)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
java.lang.AssertionError: unexpected type: <nulltype>
        at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:531)
        at com.sun.tools.javac.tree.TreeMaker.Type(TreeMaker.java:525)
        at com.sun.tools.javac.comp.Lower.BoxArgs(Lower.java:2510)
        at com.sun.tools.javac.comp.Lower.visitApply(Lower.java:2420)
        at com.sun.tools.javac.tree.Tree$Apply.accept(Tree.java:813)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.comp.Lower.visitSelect(Lower.java:3019)
        at com.sun.tools.javac.tree.Tree$Select.accept(Tree.java:987)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.comp.Lower.visitApply(Lower.java:2474)
        at com.sun.tools.javac.tree.Tree$Apply.accept(Tree.java:813)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1893)
        at com.sun.tools.javac.comp.Lower.BoxArgs(Lower.java:2517)
        at com.sun.tools.javac.comp.Lower.visitApply(Lower.java:2420)
        at com.sun.tools.javac.tree.Tree$Apply.accept(Tree.java:813)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.tree.TreeTranslator.visitExec(TreeTranslator.java:227)
        at com.sun.tools.javac.tree.Tree$Exec.accept(Tree.java:728)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.tree.TreeTranslator.translate(TreeTranslator.java:54)
        at com.sun.tools.javac.tree.TreeTranslator.visitBlock(TreeTranslator.java:145)
        at com.sun.tools.javac.comp.Lower.visitBlock(Lower.java:2933)
        at com.sun.tools.javac.tree.Tree$Block.accept(Tree.java:535)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.tree.TreeTranslator.visitMethodDef(TreeTranslator.java:129)
        at com.sun.tools.javac.comp.Lower.visitMethodDefInternal(Lower.java:2267)
        at com.sun.tools.javac.comp.Lower.visitMethodDef(Lower.java:2186)
        at com.sun.tools.javac.tree.Tree$MethodDef.accept(Tree.java:478)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.comp.Lower.visitClassDef(Lower.java:1989)
        at com.sun.tools.javac.tree.Tree$ClassDef.accept(Tree.java:434)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1881)
        at com.sun.tools.javac.comp.Lower.translate(Lower.java:1901)
        at com.sun.tools.javac.comp.Lower.translateTopLevelClass(Lower.java:3070)
        at com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:487)
        at com.sun.tools.javac.main.Main.compile(Main.java:592)

所以我遵循编译器的建议,并对Oracle的bug数据库进行了一些搜索,并通过搜索“java.lang.AssertionError:unexpected type:”来找到错误.你可以在这里查看:Primitive arrays and varargs inference leads to crash in TreeMaker.Type(TreeMaker.java:531)

使用Float包装类是您发现的工作.

EDIT – 第二个编译器错误的答案

我相信第二个编译器错误是一个错误,我已经提交到Bug Database

我能够自己编译下列列表,键和比较器,没有编译错误

List<float[]> list=new ArrayList<float[]>();
list.add(new float[] {0.0f});
list.add(new float[] {1.0f});

float[] key = new float[2];

Comparator<float[]> c = new Comparator<float[]>() {
    public int compare(float[] f1,float[] f2) { 
        return f1.length - f2.length;
    }
};

当我尝试运行“Collections.binarySearch(list,c)”时,我得到“找不到符号”的错误.如果我将“float”更改为“Float”,那么这两种情况都可以编译.

我相信上面列出的列表,关键字和比较我完全按照Java 5 spec on it完成二进制搜索合同

原文链接:https://www.f2er.com/java/124321.html

猜你在找的Java相关文章