问题描述
我正在尝试为名为clients的集合实现 AbstractTableModel
但我仍然收到add方法的错误required variable found value。
这是我的代码:
I'm trying to implement an AbstractTableModel
for a collection named "clients" but I keep receiving the error "required variable found value" for the "add" method.Here is my code:
我很抱歉造成了混乱。 add方法是为了在表中添加一个新客户端(我指的是一个新行)。我不想在集合中添加新客户端。
I'm sorry for the confusion created. The add method is meant to add a new client in the table (by that I mean a new row). I don't want to add a new client to the collection.
class ModelTabel extends AbstractTableModel{
public int getRowCount() {
return clients.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
Client c = clients.get(row-1);
switch(column){
case 0: return c.getName();
case 1: return c.getSurname();
case 2: return c.getID();
case 3: return c.getPhone();
default:return "ERROR";
}
}
public void add(Client c) {
clients.get(clients.size()++) = a;
fireTableDataChanged();
}
}
推荐答案
我相信这是与相同的问题......你的变量赋值是相反的。
I believe this is the same problem as this question... your variable assignment is reversed.
它应该是(虽然这段代码仍然不正确 - 见下文):
It should be (although this code is still incorrect - see below):
a = clients.get(clients.size()++);
编辑:Prabhakaran已经回答了这个问题,但显然人们觉得有必要对我的答案进行评价。我想我确实解决了原来的问题,但我很欣赏我的代码示例仍然不正确,所以我会尝试提供更完整的答案:
this was already answered by Prabhakaran, but apparently people felt the need to downvote my answer.. I think I did address the original question, but I appreciate that my code sample was still incorrect, so I will try to provide a more complete answer:
首先,至于未找到所需变量错误,如果你谷歌它你会看到其他SO问题作为第一个命中。 clients.get(clients.size()++)
不是变量,因此您无法为其分配内容。我不确定在你的代码中声明了 a
,但是假设它是一个变量,因此我建议撤销赋值。
First of all, as to the "required variable not found" error, if you google it you will see that other SO question as the first hit. clients.get(clients.size()++)
is not a variable, so you can't assign things to it. I am not sure where a
is declared in your code, but assume that it is a variable and thus my suggestion of reversing the assignment.
接下来,对于 clients.get(clients.size()++)
行,正如其他人提到或暗示的那样 - VAR ++
相当于 VAR = VAR + 1
,因此再次进行分配操作。在这种情况下, clients.size()
不是变量,因此您无法递增它。如果你想要 clients.size()+ 1
索引,你可以编写: a = clients.get(clients.size()+ 1 )
...但是这将抛出 ArrayIndexOutOfBoundsException
,因为你试图访问客户端的元素
超出当前大小!
Next, for the clients.get(clients.size()++)
line, as others have mentioned or alluded to - VAR++
is equivalent to VAR = VAR + 1
, so again is an assignment operation going on. In this case, clients.size()
is not a variable, so you can not increment it. If you wanted the clients.size() + 1
index, you could have written: a = clients.get(clients.size() + 1)
... but that will throw an ArrayIndexOutOfBoundsException
, because you're trying to access an element of clients
beyond its current size!
这就是Prabhakaran重写你的方法的原因,将该行更改为 clients.add (c)
调用 - 因为它似乎符合该方法的原始意图。
That is why Prabhakaran rewrote your method as they did, changing that line to a clients.add(c)
call - as it seemed to fit the original intent of the method.
这篇关于为Java集合实现AbstractTableModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!