将数据从JTable复制到ArrayList [英] Copy Data from JTable into ArrayList
问题描述
如何将数据从JTable复制到ArrayList? 我不希望第一行是列名,我希望其他所有行.谢谢
Hi how can I copy data from my JTable into an ArrayList? I dont want the first row which is the columns name, I want everyother row. Thanks
推荐答案
如果不使用任何对象将数据存储在表中,则只需在表上逐行遍历并获取每个对象并将其存储在列表中
If you are storing the data in the table with out using any object then just traverse row by row on the table and get every object and store it in the List.
示例:这是当您使用列表存储数据时.
Example: This is when you are using a list to store the data.
List<Object> list = new ArrayList<Object>;
for(int row = 0; row < table.getRowCount(); row++) {
for(int column = 0; column = table.getColumnCount(); column++) {
list.add(table.getValueAt(row, column));
}
}
最后,您将获得列表中的所有对象.
Finally you will get all the objects in the list.
由于每一行都是CustomObject
类型的对象,我们需要将表的每个值设置为CustomObject
类型的对象,并将其添加到每一行的列表中.
Since each row is an object of type CustomObject
we need to set each value of the table to the object of type CustomObject
and add it to the List for every row.
class CustomObject {
Object one;
Object two;
}
List<CustomObject> list = new ArrayList<CustomObject>;
for(int row = 0; row < table.getRowCount(); row++) {
CustomObject object = new CustomObject();
for(int column = 0; column = table.getColumnCount(); column++) {
switch(column) {
case 0:
object.setOne(table.getValueAt(row, column));
break;
case 1:
object.setTwo(table.getValueAt(row, column));
break;
}
}
// Store each object in the list.
list.add(object);
}
这篇关于将数据从JTable复制到ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!