将 ArrayList 转换为二维数组 [英] Converting an ArrayList into a 2D Array

查看:211
本文介绍了将 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());
        }
    }

这篇关于将 ArrayList 转换为二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆