-3

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.

Fade
  • 13
  • 1
  • 3
  • 2
    What would you expect the contents of `objArray` to be? – Jeffrey Aug 10 '13 at 01:58
  • As others have said, this doesn't make sense. You'll need to explain how you want to distribute the elements of your List into two groups that match the structure of a 2 dimensional array. – jahroy Aug 10 '13 at 02:11
  • You can convert a List to an array like this: `myList.toArray(new Object[0]);` – jahroy Aug 10 '13 at 02:13
  • I think that the Answer to your EDIT3 version of your Question is either "that's bad design ... don't do it" or "Your Question answers itself, and is therefore redundant". Either way, I don't see the value of reopening it. – Stephen C Aug 10 '13 at 03:06

1 Answers1

1

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.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • depends what you mean by convert – aaronman Aug 10 '13 at 01:57
  • 1
    @aaronman - Yes, and the OP needs to define what _he_ means by convert. – jahroy Aug 10 '13 at 02:11
  • I attempted to clarify my question. – Fade Aug 10 '13 at 02:25
  • @Fade, After reading the updated question, I want to tell you that you don't need a 2d array. You are getting an array of objects, simply retrive the information from those objects. Or if you want to print the objects the way you have mentioned, simply override toString method in your MyObj class. You can generate toString method if using IDE like eclipse, right click on class->source-generate toString. Once you do that then print your objects in a pretty way. – Juned Ahsan Aug 10 '13 at 02:36
  • @JunedAhsan Thank you for your reply. I will update my original question. – Fade Aug 10 '13 at 02:49