本文介绍了如何创建只有一个数组的对象数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码片段,我不明白为什么不工作:
I have the following code snippet, which I can't understand why doesn't work:
List<Object[]> listOfObjectArrays; listOfObjectArrays = new ArrayList<>(); Object[] objectArray = new Object[] {1, "two", null}; listOfObjectArrays.add(objectArray); // works just fine listOfObjectArrays = Arrays.asList(objectArray, objectArray); // works just fine listOfObjectArrays = Arrays.asList(objectArray); // * // compile error: Incompatible types. Required: List<java.lang.Object[]> Found: List<java.lang.Object> listOfObjectArrays = Arrays.asList(new Object[] {1, "two", null}); // compile error: Incompatible types. Required: List<java.lang.Object[]> Found: List<java.lang.Object>有人可以请我指向正确的方向吗?
Could somebody please point me in the right direction?
我已经看到 Jon Skeet的回答另一个问题,但最后一个例子对我没有效果。即使我在标有的行中向 Object 或 Object [] * 我得到一个编译错误。
I already saw Jon Skeet's answer on an other question, but the last example there does not work for me. Even if I add a cast to either Object or Object[] in the line marked with * I get a compile error.
推荐答案Object [] 通过明确指定类型参数:
You can always tell Java that you want a list of Object[] by specifying the type parameter explicitly:
Object[] objectArray = { 1, "two", null }; List<Object[]> listOfObjectArrays = Arrays.<Object[]>asList(objectArray);更多推荐
如何创建只有一个数组的对象数组列表?
发布评论