[英]Array stored in a linked list. (Java)
Is it possible to store a whole array as a node in a linked list.是否可以将整个数组存储为链表中的节点。 I am using the Linked List collection java provides and I keep getting an error for the following code.
我正在使用 java 提供的链接列表集合,并且我不断收到以下代码的错误。
List<Object[]> params = new LinkedList<Object[]>();
params.add(new Object[] { "ahhsjhs", {"jsdjdsk","djksdjsdk"}, true});
Type mismatch: cannot convert from String[] to Object类型不匹配:无法从 String[] 转换为 Object
You can use this and not get error您可以使用它而不会出错
List<Object[]> params = new LinkedList<Object[]>();
params.add(new Object[]{"ahhsjhs", new String[]{"jsdjdsk", "djksdjsdk"}, true});
You can even do this and there is nothing wrong你甚至可以这样做,没有错
List<Object[]> params = new LinkedList<Object[]>();
params.add(new Object[]{"ahhsjhs", new Object[]{new Object[] {"@@", new Object[] {"@@"},"@@"}, "@@"}, true});
But it is bad practice.但这是不好的做法。 You should approach to OOP.
您应该接近 OOP。
Just put this String[] objects
:只需放置这个
String[] objects
:
List<Object[]> params = new LinkedList<Object[]>();
String[] objects = new String[] { "jsdjdsk", "djksdjsdk" };
params.add(new Object[] { "ahhsjhs", objects, true });
It is possible, and you're doing it (almost) correctly.这是可能的,并且您(几乎)正确地做到了。 Compiler get confused with the inline declaration, doing the declaration outside should get you past the error, something like the following:
编译器对内联声明感到困惑,在外部进行声明应该可以让您摆脱错误,如下所示:
List<Object[]> params = new LinkedList<Object[]>();
String[] a = new String[]{"jsdjdsk","djksdjsdk"};
params.add(new Object[] { a });
params.add(new Object[] { "ahhsjhs", true});
System.out.println(params);
}
This with java:8.这与 java:8。
I don't really see why you would want to do this but removing the inner braces might do the trick, that is:我真的不明白你为什么要这样做,但删除内括号可能会奏效,即:
List<Object[]> params = new LinkedList<>();
params.add(new Object[]{"ahhsjhs","jsdjdsk","djksdjsdk",true});
(I would have posted this as a comment but I don't have enough reputation) (我会将此作为评论发布,但我没有足够的声誉)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.