FoufaFaFa FoufaFaFa - 1 month ago 7
Java Question

create an object[ ] from 2 list in java

I have 2 list, I want to create an object[]from this 2 list:

List<Object[]> l1 = new ArrayList<>();
l1.add(new Object[] { "itemP1", "child1", });
l1.add(new Object[] { "itemP2", "child2", });
l1.add(new Object[] { "itemP3", "child3", });
List<String> l2 = new ArrayList<>();
l2.add("item");
final Object[] inventory = new Object[] { "item", new Object[] { "itemP1", "child1", },
new Object[] { "itemP2", "child2", }, new Object[] { "itemP3", "child3", }, };


but by looping l1 and l2 not manually . have any idea please?

Answer

Something like

List<Object> finalList= new ArrayList<Object>(l1);
finalList.addAll(l2);
Object[] finalArray = finalList.toArray(new Object[finalList.size()]);
Comments