gpt4 book ai didi

java - 列表 : set method causing problems - IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-30 04:08:57 26 4
gpt4 key购买 nike

当我尝试调用 tableUpdate 方法时,出现 IndexOutOfBoundsException
我认为我使用 set() 方法的方式有问题。我尝试过像这样 int row = Members.size() -1 放置 a -1 但这并没有解决问题。你能帮我么?

class MyTableModel extends AbstractTableModel {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Member> members = new ArrayList<Member>();

public MyTableModel(List<Member> members) {
this.members = members;
}

public void addMember(Member m) {
members.add(m);
int row = members.size() - 1;
fireTableRowsInserted(row, row);
}

public void removeMemberFromTable(int row) {
members.remove(row);
fireTableRowsDeleted(row, row);
}

public void tableUpdate(int row, Member m) {
row = members.size();
members.set(row, m);
fireTableRowsUpdated(row, row);
}

public List<Member> getMembers() {
return new ArrayList<Member>(members);
}

@Override
public int getColumnCount() {
return 8;
}

@Override
public int getRowCount() {
return members.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Member m = members.get(rowIndex);

switch (columnIndex) {
case 0:
return m.getMemberId();

case 1:
return m.getfName();

case 2:
return m.getlName();

case 3:
return m.getAddress();

case 4:
return m.getZipCode();

case 5:
return m.getCity();

case 6:
return m.getEmail();

case 7:
return m.getPhoneNo();

default:
return "";
}
}

public String getColumnName(int column) {
switch (column) {
case 0:
return "MedlemsID";

case 1:
return "Fornavn";

case 2:
return "Efternavn";

case 3:
return "Adresse";

case 4:
return "Postnummer";

case 5:
return "By";

case 6:
return "Email";

case 7:
return "Telefonnr";
default:
return "";
}
}

public Class<?> getColumnClass(int columnIndex) {
switch (columnIndex) {
case 0:
return String.class;

case 1:
return String.class;

case 2:
return String.class;

case 3:
return String.class;

case 4:
return String.class;

case 5:
return String.class;

case 6:
return String.class;

case 7:
return String.class;

default:
return String.class;
}
}

异常

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.set(ArrayList.java:397)
at gui.MyTableModel.tableUpdate(ShowMembersGUI.java:179)
at gui.EditMemberGUI$1.actionPerformed(EditMemberGUI.java:108)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3312)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

您想要替换最后一个元素之后的元素。这没有道理。也许您想检查索引是否在范围内,如下所示:

public void tableUpdate(int row, Member m) {
if (row < members.size()) {
members.set(row, m);
fireTableRowsUpdated(row, row);
}
}

关于java - 列表 : set method causing problems - IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20096573/

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