【问题标题】:Delete an object from Arraylist从 Arraylist 中删除一个对象
【发布时间】:2014-05-04 05:55:47
【问题描述】:

我有一个对象(名称、一些数字等)的 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
            }
        }

    }

我错过了什么? 感谢您的帮助。

【问题讨论】:

  • 请遵循 Java 大小写约定。另外,Name 的值是多少,您确定您的 if 条件计算结果为true?你试过调试器吗?
  • 可以用Vector代替ArrayList吗? DefaultTableModel 有一个 constructor for Vector,它将使其他一切变得更加简单。

标签: java swing arraylist iterator actionlistener


【解决方案1】:

让我们从这里开始......

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

基本上,您会获得选定行的数量,但您只使用第一个选定行的索引...table.getSelectedRow()

来自JavaDocs...

返回:
第一个选定行的索引

你应该做的是

for(int i : table.getSelectedRows()) {

这将遍历每个选定的索引。

你应该避免这样做......

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,从AbstractTableModel 延伸,它环绕着你的ArrayList...

【讨论】:

    【解决方案2】:

    如果你只想删除一行就行了

      myarraylistofobjects.remove(selectedRow) ;
    
      tablemodel.removeRow(table.getSelectedRow());
    

    这两行将解决您的问题

    【讨论】:

      猜你喜欢
      • 2016-02-14
      • 2021-03-11
      • 2015-08-02
      • 1970-01-01
      • 2015-08-05
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多