转换一个ArrayList到一个二维数组 [英] Converting an ArrayList into a 2D Array
问题描述
在Java中,你如何转换的ArrayList到一个二维数组对象[] []?
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屋!