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.
List<Object[]> params = new LinkedList<Object[]>();
params.add(new Object[] { "ahhsjhs", {"jsdjdsk","djksdjsdk"}, true});
Type mismatch: cannot convert from String[] to 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.
Just put this 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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.