In Java how do you convert a ArrayList into a two dimensional array Object[][]?
在Java中如何将ArrayList转换为二维数组Object [] []?
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
来自评论:我将通过更多详细信息向您描述问题:XML文件包含联系人列表(例如姓名,地址......)。我可以获取此信息的唯一方法是通过ArrayList,它将提供给我。因为我需要以有序的方式将这个数组列表的内容存储在Java Swing表中,所以我想将它转换为二维对象数组
7 个解决方案
#1
8
I presume you are using the JTable(Object[][], Object[])
constructor.
我假设您正在使用JTable(Object [] [],Object [])构造函数。
Instead of converting an ArrayList<Contact>
into an Object[][]
, try using the JTable(TableModel)
constructor. You can write a custom class that implements the TableModel
interface. Sun has already provided the AbstractTableModel
class for you to extend to make your life a little easier.
不要将ArrayList
public class ContactTableModel extends AbstractTableModel {
private List<Contact> contacts;
public ContactTableModel(List<Contact> contacts) {
this.contacts = contacts;
}
public int getColumnCount() {
// return however many columns you want
}
public int getRowCount() {
return contacts.size();
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0: return "Name";
case 1: return "Age";
case 2: return "Telephone";
// ...
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
Contact contact = contacts.get(rowIndex);
switch (columnIndex) {
case 0: return contact.getName();
case 1: return contact.getAge();
case 2: return contact.getTelephone();
// ...
}
}
}
Later on...
List<Contact> contacts = ...;
TableModel tableModel = new ContactTableModel(contacts);
JTable table = new JTable(tableModel);
#2
2
The simple way is to add a method to the Contact
like this:
简单的方法是向Contact添加一个方法,如下所示:
public Object[] toObjectArray() {
return new Object[] { getName(), getAddress, /* ... */ };
}
and use it like this:
并像这样使用它:
ArrayList<Contact> contacts = /* ... */
Object[][] table = new Object[contacts.size()][];
for (int i = 0; i < contacts.size(); i++) {
table[i] = contacts.get(i).toObjectArray();
}
#3
2
Try this:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
int[][] a = new int[list.size()][list.size()];
for(int i =0; i < list.size(); i++){
for(int j =0; j <list.size(); j++){
a[i][j]= list.get(j +( list.size() * i));
}
}
#4
1
I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts
我知道每个联系人拥有的属性数量,我设法找到“一种方法”(6)。所以考虑一个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());
}
}
#5
0
What you really want is to sort the ArrayList. To do that your Contacts class must implement a Comparator method.
你真正想要的是对ArrayList进行排序。为此,您的Contacts类必须实现Comparator方法。
Check the next page for an example: http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example
查看下一页的示例:http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example
#6
0
I will recommend that you parse your XML into java objects and store the object in a custom data object. This will make it easier for you to do many operations on the available data.
我建议您将XML解析为java对象并将对象存储在自定义数据对象中。这将使您更容易对可用数据执行许多操作。
Here is small tutorial on how to do it.
这是关于如何做到这一点的小教程。
#7
0
public static String[][] convertListIntoArrayObj(List<TeamMenuSelected> possibilities) {
int numberOfColums = 2;
int numberOfRows = possibilities.size();
String[][] values = new String[numberOfRows][numberOfColums];
for(int x=0; x<possibilities.size(); x++) {
TeamMenuSelected item = possibilities.get(x);
values[x][0] = item.getTeamName();
values[x][1] = item.getTeamCuisine();
}
return values;
}
#1
8
I presume you are using the JTable(Object[][], Object[])
constructor.
我假设您正在使用JTable(Object [] [],Object [])构造函数。
Instead of converting an ArrayList<Contact>
into an Object[][]
, try using the JTable(TableModel)
constructor. You can write a custom class that implements the TableModel
interface. Sun has already provided the AbstractTableModel
class for you to extend to make your life a little easier.
不要将ArrayList
public class ContactTableModel extends AbstractTableModel {
private List<Contact> contacts;
public ContactTableModel(List<Contact> contacts) {
this.contacts = contacts;
}
public int getColumnCount() {
// return however many columns you want
}
public int getRowCount() {
return contacts.size();
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0: return "Name";
case 1: return "Age";
case 2: return "Telephone";
// ...
}
}
public Object getValueAt(int rowIndex, int columnIndex) {
Contact contact = contacts.get(rowIndex);
switch (columnIndex) {
case 0: return contact.getName();
case 1: return contact.getAge();
case 2: return contact.getTelephone();
// ...
}
}
}
Later on...
List<Contact> contacts = ...;
TableModel tableModel = new ContactTableModel(contacts);
JTable table = new JTable(tableModel);
#2
2
The simple way is to add a method to the Contact
like this:
简单的方法是向Contact添加一个方法,如下所示:
public Object[] toObjectArray() {
return new Object[] { getName(), getAddress, /* ... */ };
}
and use it like this:
并像这样使用它:
ArrayList<Contact> contacts = /* ... */
Object[][] table = new Object[contacts.size()][];
for (int i = 0; i < contacts.size(); i++) {
table[i] = contacts.get(i).toObjectArray();
}
#3
2
Try this:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
list.add(..);
int[][] a = new int[list.size()][list.size()];
for(int i =0; i < list.size(); i++){
for(int j =0; j <list.size(); j++){
a[i][j]= list.get(j +( list.size() * i));
}
}
#4
1
I managed to find "a way" to do so, knowing the number of attributes each contacts has (6). So considering an ArrayList listofContacts
我知道每个联系人拥有的属性数量,我设法找到“一种方法”(6)。所以考虑一个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());
}
}
#5
0
What you really want is to sort the ArrayList. To do that your Contacts class must implement a Comparator method.
你真正想要的是对ArrayList进行排序。为此,您的Contacts类必须实现Comparator方法。
Check the next page for an example: http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example
查看下一页的示例:http://www.java-examples.com/sort-java-arraylist-descending-order-using-comparator-example
#6
0
I will recommend that you parse your XML into java objects and store the object in a custom data object. This will make it easier for you to do many operations on the available data.
我建议您将XML解析为java对象并将对象存储在自定义数据对象中。这将使您更容易对可用数据执行许多操作。
Here is small tutorial on how to do it.
这是关于如何做到这一点的小教程。
#7
0
public static String[][] convertListIntoArrayObj(List<TeamMenuSelected> possibilities) {
int numberOfColums = 2;
int numberOfRows = possibilities.size();
String[][] values = new String[numberOfRows][numberOfColums];
for(int x=0; x<possibilities.size(); x++) {
TeamMenuSelected item = possibilities.get(x);
values[x][0] = item.getTeamName();
values[x][1] = item.getTeamCuisine();
}
return values;
}