Java 从Arraylist中删除对象

Java 从Arraylist中删除对象,java,swing,arraylist,iterator,actionlistener,Java,Swing,Arraylist,Iterator,Actionlistener,我有一个对象名的ArrayList,一些数字,等等,我可以打开并在JTable名称上看到,等等。我可以将一个对象添加到JTable并将其添加到ArrayList。当我试图从JTable中删除一个对象时,它也不会在我的ArrayList中删除。我制作了这个ActionListener,并尝试了使用remove和迭代器删除对象的两种方法 class ButtonRemovePersoAL implements ActionListener { public void acti

我有一个对象名的ArrayList,一些数字,等等,我可以打开并在JTable名称上看到,等等。我可以将一个对象添加到JTable并将其添加到ArrayList。当我试图从JTable中删除一个对象时,它也不会在我的ArrayList中删除。我制作了这个ActionListener,并尝试了使用remove和迭代器删除对象的两种方法

    class ButtonRemovePersoAL implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            int numerorows = table.getSelectedRows().length;
            for(int i=0; i < numerorows ; i++ ) {
                String Name = (String) table.getModel().getValueAt(table.getSelectedRow(), 0); // I search for the first case of the JTable to catch the Object to erase
                for(Object object : myarraylistofobjects) {
                    if(Name.equals(object.getName())) {
                        myarraylistofobjects.remove(object);
                    }
                 }                  
                                  // OR
                Iterator<Object> itr = myarraylistofobjects().iterator();
                while (itr.hasNext()) {
                    Object object = itr.next();
                       if (Name.equals(object.getName())) {
                       itr.remove();
                    }

                }

                tablemodel.removeRow(table.getSelectedRow()); // I delete finally my row from the jtable
            }
        }

    }
我错过了什么? 谢谢你的帮助。

让我们从这里开始

int numerorows = table.getSelectedRows().length;
for(int i=0; i < numerorows ; i++ ) {
    String Name = (String) table.getModel().getValueAt(table.getSelectedRow(), 0); // I search for the first case of the JTable to catch the Object to erase
它将循环遍历每个选定的索引

你应该避免这样做

String Name = (String) table.getModel().getValueAt(table.getSelectedRow(), 0);
由于视图可能会被排序,这意味着视图索引所选行不会直接映射到模型行,因此您应该使用

String name = (String) table.getValueAt(i, 0);
从这里开始,一切都变得有点混乱

当你做一些像

tablemodel.removeRow(table.getSelectedRow());
所有索引都不再有效,更不用说您不应该使用table.getSelectedRow

相反,当您从ArrayList中删除该项时,应该记下它,然后遍历TableModel删除删除列表中的任何项

例如

List<String> removedNames = new ArrayList<String>(25);
for(int i : table.getSelectedRows() ) {
    String name = (String) table.getValueAt(i, 0);
    removedNames.add(name);
    //...
}

int index = 0;
while (index < tableModel.getRowCount()) {
    Object value = tableModel.valueAt(index, 0);
    if (value != null && removedNames.contains(value.toString()) {
        tableModel.removeRow(index);
    } else {
        index++;
    }
}

坦率地说。我认为更简单的解决方案是创建一个自定义的TableModel,它是从环绕ArrayList的AbstractTableModel扩展而来的…

如果您只想删除一行的话

  myarraylistofobjects.remove(selectedRow) ;

  tablemodel.removeRow(table.getSelectedRow());

这两行将解决您的问题

请遵循Java大写惯例。另外,Name的值是多少,您确定if条件的计算结果为true吗?您是否尝试过调试器?是否可以使用Vector而不是ArrayList?DefaultTableModel有一个新的模型,它将使其他一切变得更加简单。
  myarraylistofobjects.remove(selectedRow) ;

  tablemodel.removeRow(table.getSelectedRow());