如何在 java 中像 "List<Object[]> listObject" 这样的变量列表添加元素?
How to add element to that variable list like this "List<Object[]> listObject" in java?
如果squareBracket中的数据类型是数组,我不知道如何添加元素
如果您想在列表中添加元素,只需执行以下操作:
listObject.add(new Object[]{});
或者如果你想添加到列表中的对象数组然后使用(由于数组数据结构的限制行为,这是一个相当长的根,这就是为什么 ArrayList 是实现此目的的替代方法。):
@Test
public void testArray() {
List<Object[]> listObjects = new ArrayList<>();
listObjects.add(new Object[]{1,2});
addX( listObjects.get(0).length , listObjects.get(0) , 3);
listObjects.set(0,addX( listObjects.get(0).length , listObjects.get(0) , 3));
System.out.println(listObjects.get(0));
}
public Object[] addX(int n, Object arr[], Object x)
{
int i;
// create a new array of size n+1
Object newarr[] = new Object[n + 1];
// insert the elements from
// the old array into the new array
// insert all elements till n
// then insert x at n+1
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}
如果squareBracket中的数据类型是数组,我不知道如何添加元素
如果您想在列表中添加元素,只需执行以下操作:
listObject.add(new Object[]{});
或者如果你想添加到列表中的对象数组然后使用(由于数组数据结构的限制行为,这是一个相当长的根,这就是为什么 ArrayList 是实现此目的的替代方法。):
@Test
public void testArray() {
List<Object[]> listObjects = new ArrayList<>();
listObjects.add(new Object[]{1,2});
addX( listObjects.get(0).length , listObjects.get(0) , 3);
listObjects.set(0,addX( listObjects.get(0).length , listObjects.get(0) , 3));
System.out.println(listObjects.get(0));
}
public Object[] addX(int n, Object arr[], Object x)
{
int i;
// create a new array of size n+1
Object newarr[] = new Object[n + 1];
// insert the elements from
// the old array into the new array
// insert all elements till n
// then insert x at n+1
for (i = 0; i < n; i++)
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
}