Converting an ArrayList into a 2D Array

前端 未结 8 2226
北海茫月
北海茫月 2020-12-04 01:55

In Java how do you convert a ArrayList into a two dimensional array Object[][]?

From comments: I will describe you the problem with more details: an XML file inclu

8条回答
  •  半阙折子戏
    2020-12-04 02:32

    The simple way is to add a method to the Contact like this:

    public Object[] toObjectArray() {
        return new Object[] { getName(), getAddress, /* ... */ };
    }
    

    and use it like this:

    ArrayList contacts = /* ... */
    Object[][] table = new Object[contacts.size()][];
    for (int i = 0; i < contacts.size(); i++) {
        table[i] = contacts.get(i).toObjectArray();
    }
    

提交回复
热议问题