gpt4 book ai didi

java - 从数组列表填充表

转载 作者:行者123 更新时间:2023-12-01 14:47:30 27 4
gpt4 key购买 nike

我要填写表格y数据存储在 arraylist 中。我有三个数组列表:bornesNom , bornesXbornesY ,该表包含三列 Nom , XY

我想设置TableModel但不知道如何。

该表基于此模型:

TableModel  bornesTableModel = new DefaultTableModel(
new String[][] { { "One", "Two","Two" }, { "Three", "Four", "Four" } },
new String[] { "Nom", "X", "Y" });

最佳答案

根据您的评论,我认为实际问题是如何将 3 个列表转换为二维数组。

除此之外,拥有 3 个单独的列表似乎是您设计中的一个严重缺陷(您最好有一个包含一个实例的数据的对象列表),我会尝试给您一个提示:

创建一个二维数组,其第一维的大小与列表的大小相同。然后同时循环所有列表并提取给定索引处的数据。创建并填充一个长度为 3 的 String 数组,并将其分配给外部数组的索引。

我将提供一个小示例,但请记住,您必须处理列表不匹配的情况。

基本上它可能看起来像这样:

List<String> listA = ...;
List<String> listB = ...;
List<String> listC = ...;

//note: the lists could have different lengths so this is unsafe
//I'll leave this as an excercise for you
int listLength = listA.size();

String array[][] = new String[listLength][];

for( int i = 0; i < listA.size(); i++ )
{
array[i] = new String[3];
array[i][0] = listA.get( i );
array[i][1] = listB.get( i );
array[i][2] = listC.get( i );
}



另一种选择可能是根据 3 个列表推出您自己的 TableModel,但在此之前,请尝试使用正确的数据结构替换列表。

关于java - 从数组列表填充表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274735/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com