如何创建只有一个数组的对象数组列表?

编程入门 行业动态 更新时间:2024-10-28 16:30:04
本文介绍了如何创建只有一个数组的对象数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码片段,我不明白为什么不工作:

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);

更多推荐

如何创建只有一个数组的对象数组列表?

本文发布于:2023-08-04 03:02:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   只有一个   对象   列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!