What is the easiest way to convert a Java ArrayList to Object[][]?
For example:
List<MyClass> myList = new ArrayList<MyClass>();
myList.add(myObj1);
myList.add(myObj2);
Object[][] objArray = myList.... How do I convert?
The reason I'm trying to do this is to use the QueryRunner.batch(String sql, Object[][] params) method of DBUtils.
EDIT: See here for details: DBUtils QueryRunner.batch()
EDIT2:
I'll try to give some more information.
public class MyObj
{
int myInt;
String myString;
}
MyObj obj1 = new MyObj(1, "test1");
MyObj obj2 = new MyObj(2, "test2");
List<MyObj> myList = new ArrayList<MyObj>();
myList.add(obj1);
myList.add(obj2);
Object[] onedArray = myList.toArray(); // Now I have a 1d array of my list of objects.
Object[] objArray = myList.get(0); // How do I convert each object instance into an array of Objects?
// Intended result would be something like this:
new Object[][] { { 1, "test1" }, { 2, "test2" } };
EDIT3: One possible solution is this: I could add a toObjectArray() method to MyObj class. Surely there must be a better way?
public Object[] toObjectArray()
{
Object[] result = new Object[2];
result[0] = this.myInt;
result[1] = this.myString;
return result;
}
Thanks.
Arraylist is a single dimensional collection because it uses a single dimensional array inside. You cannot convert a single dimensional array to a two dimensional array.
You may have to add more information in case you want to do conversion of 1D to 2D array.