当我运行此代码时
List<int[]> list = Arrays.asList(new int[]{1, 2, 3}, new int[]{4, 5});
int[][] arr = list.stream().map(j -> j.clone()).toArray(int[][]::new);
System.out.println(Arrays.deepToString(arr));
它按预期工作,我得到输出
[[1, 2, 3], [4, 5]]
但是,如果我用clone()
int[][] arr = list.stream().map(int[]::clone).toArray(int[][]::new);
我收到运行时异常:
Exception in thread "main" java.lang.NoClassDefFoundError: Array
at Main.lambda$MR$main$clone$8ed4b78b$1(Main.java:14)
at Main$$Lambda$1/1160460865.apply(Unknown Source)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:512)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:502)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:576)
at java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:255)
at java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:438)
at Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: Array
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
我根本不明白这一点。我原以为方法参考和lambda是等价的。此外,它似乎是一个非常奇怪的例外。谁能解释一下?
我使用的是JDK 1.8.0_25。
答案 0 :(得分:11)
这是错误JDK-8056051。它在8u40中被列为固定。
基于bug注释,javac在编译期间的某个时刻使用了一个伪Array
类,并忘记将其重写为实际的数组类,从而导致此错误。