gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:58:45 25 4
gpt4 key购买 nike

我正在尝试使用 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, key, 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 的错误数据库中进行了一些搜索,并通过搜索“java.lang.AssertionError: unexpected type:”找到了错误。您可以在这里查看:Primitive arrays and varargs inference leads to crash in TreeMaker.Type(TreeMaker.java:531)

如您所见,可以使用 Float 包装类。

编辑 - 第二个编译器错误的答案

我认为第二个编译器错误是一个错误,我已将其提交给 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, key, c)”时,出现“找不到符号”错误。如果我将“float”更改为“Float”,它在这两种情况下都能正常编译。

我相信我上面发布的列表、键和比较器完全根据 Java 5 spec on it 完成二进制搜索契约(Contract)。

关于java - 在 Java 5 中使用原始数组作为实际类型参数有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com