我有一个要从中删除对象的向量。该方法如下所述(以及确保将其删除的检查)。它调用定义向量存储位置的 getVector 方法和返回向量内的对象的 findItem。我知道 getVector 和 findItem 可以正常工作,因为我能够将对象添加到向量并查看向量中的对象。
public Message removeMember (Message m)
//list is the vector location
//item is the object in the vector i am looking to remove
{
list = getVector(m);
item = findItem(list, m);
if (item != null) // if item exists in the vector (code enters this statement)
{
System.out.println("removing " + item.name + " from file");
list.remove(item); // remove the item from the vector
// search for item to insure it is removed
item = findItem(list, m);
if (item == null) // if item has been removed
{
System.out.println(m.name + " removed from file.");
m.response = m.name + " removed from file.";
}
else //if the item has not been removed
{
System.out.println("Error removing member from file.");
m.response = "Error removing member from file.";
}
}
else // item does not exist in vector at all
{
m.response = m.name + " was previously removed or never existed.";
}
return m;
}
我遇到的问题是,即使我进行检查以确保删除该项目,并且程序进入该检查并且 System.out.println(m.name + " removed from file.") 在控制台上进行验证该条目已被删除,我仍然可以搜索它(通过程序),当我打开矢量位置,浏览文本文件时,我可以看到信息仍在文件中。
预先感谢您的帮助!