我正在使用一个API,我需要在其中提供
Object[]
,
Object[][]
,
Object[][][]
…你明白了。
假定
setFoo
需要一个
对象[ ]
,这就是我让它工作的方式:
Object hello = "Hello";
Object world = "world";
final List<Object> objects = new ArrayList<>();
objects.add(hello);
objects.add(world);
// {"Hello", "world"}
item.setFoo(objects.toArray());
我就是这样做的
对象[][]
要求工作,这样我可以打电话
setBar
…
Object hello = "Hello";
Object world = "world";
// We now we need to stuff these into an Array of Arrays as in: {{Hello},{world}}
final List<Object> helloList = new ArrayList<>();
helloList.add(hello);
final List<Object> worldList = new ArrayList<>();
worldList.add(world);
final List<List<Object>> surroundingList = new ArrayList<>();
surroundingList.add(helloList);
surroundingList.add(worldList);
final Object[][] objects = new Object[surroundingList.size()][1];
for (int i = 0; i < surroundingList.size(); i++) {
objects[i] = surroundingList.get(i).toArray();
}
item.setBar(objects);
问题是,我无法弄清楚如何动态创建对象[][]。有没有办法在Java中做到这一点?如果我能让他
final Object[][] objects = new Object[surroundingList.size()][1];
我应该掌握好动态。