java table集合,为Java集合实现AbstractTableModel

java table集合,为Java集合实现AbstractTableModelI’mtryingtoimplementanAbstractTableModelforacollectionnamed”clients”butIkeepreceivingtheerror”requiredvariablefoundvalue”forthe”add”method.Hereismycode:I’msorryforthecon…

java table集合,为Java集合实现AbstractTableModel

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:

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()++);

EDIT: 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:

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.

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!

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 table集合,为Java集合实现AbstractTableModel分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/30977.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注