我正在尝试用 objects 的 LinkedList 中保存的数据填充 JTable

I am trying to make a JTable populated with data held in a LinkedList of objects

我有标题,我想遍历一个二维数组,这样它就能保存所有数据。

String [] columnNames ={"Name", "Day" , "Month" , "Year"};

Object[][] info = new Object [7][newList.size()-1];

for (int i = 0; i<newList.size(); i++)
{
    info[i][0] = { ""+ newList.get(i).getName() }; 
    info[i][1] = { ""+ newList.get(i).getDay() }; 
}

...等等。

但是,这会显示错误:

数组常量只能在初始化器中使用。

我该如何解决这个问题?

我以后打算用下面的方式展示这个:

JTable JTable table = new JTable(info, columnNames);
JOptionPane.showMessageDialog(null, new JScrollPane(table),       
  "List",       
  JOptionPane.INFORMATION_MESSAGE     
);  

这里:

info[i][0] = { ""+ newList.get(i).getName() }; 

只需省略 { }

重点是:info[i][0] 不是 数组。当对 two-dim 数组使用 two 索引时,你已经 "addressing" 一个 cell in your table .

所以,只需要:

info[i][0] = newList.get(i).getName().toString();

(假设 getName() not return 已经是一个字符串;无论如何 "" + 这里也根本不需要)

您只在 one shot 中分配数组时使用 { },例如

String strs[] = { "first", "second" };

例如!