将 ArrayList 转换为二维数组

将 ArrayList 转换为二维数组

问题描述:

Java 中如何将 ArrayList 转换为二维数组 Object[][]?

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

来自评论:我将更详细地向您描述问题:XML 文件包含联系人列表(例如姓名、地址...).我能获得这些信息的唯一方法是通过一个 ArrayList,它将提供给我.由于我需要以有序的方式将此数组列表的内容存储在 Java Swing 表中,因此我想将其转换为对象的二维数组

From comments: I will describe you the problem with more details: an XML file includes a list of contacts (e.g. name, address...). The only way I can obtain this information is through an ArrayList, which will be given to me. As I need to store the content of this array list in a Java Swing table in an ordered manner, I was thinking to convert it into a two dimensional array of objects

我设法找到了一种方法",知道每个联系人具有的属性数量 (6).所以考虑一个 ArrayList listofContacts

I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts

    int numberOfContacts = listofContacts.size()/6;
    Object[][] newArrayContent = new Object[numberOfContacts][6];

    for(int x = 0; x<numberOfContacts; x++){
        for(int z = 0; z < 6; z++){
        int y = 6 * x;
        newArrayContent [x][z] = list.get(y+z); 
        System.out.println(newArrayContent [x][z].toString());
        }
    }