简体   繁体   English

数组存储在链表中。 (爪哇)

[英]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.

相关问题 使用Java传递指向链表的指针。 但是,当我修改内存/阵列中的数据时,链表中的数据并未更新。 为什么? - passing pointers to linked list using java. But when I modify the data in the memory/Array it's not updated in the Linked List. Why? Output 不会打印链表中的所有列表。 使用 java - Output does not print all of the list in linked list. Using java 删除循环链表中的所有匹配项。 (爪哇) - Deleting all occurrences in a circular linked list. (java) 制作一个链表,其中每个节点都连接到另一个链表。(java) - Making a linked list where each node is connected to another linked list.(java) 链表Java数组 - Array of Linked List Java 使用数组列表在 Java 中创建骰子滚轮。 (不是和数组) - Creating a Dice Roller in Java with an Array List. (Not and Array) 创建形状并将其放在数组列表中。 (JAVA) - Create shapes and put it inside the array list. (Java) 链接列表。按顺序插入整数 - Linked List. Insert integers in order 存储在数组中的链接列表中的引用变量 - Referencing variable in Linked List stored in array Java LinkedHashSet 使用双向链表。 它也可以向后迭代吗? - The Java LinkedHashSet uses a doubly-linked list. Can it also be iterated backwards?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM