简体   繁体   中英

removing an entry from a vector java

I have a vector that I am trying to remove an object from. The Method is stated below (as well as the check to insure it is removed). In it calls the getVector Method that defines where the vector is stored, and findItem which returns an object inside the Vector. I know getVector and findItem work correctly due to me being able to add an object to a vector, and view objects in the vector.

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;
}

The problem I am experiencing is that even though I do the check to make sure the item is removed, and the program enters that check and the System.out.println(m.name + " removed from file.") verifies on the console that the entry has been removed, I can still search for it (through the program) and when I open the vector location, browse the text file, I can see the information is still in the file.

Thank you in advance for your help!

Unless i'm missing something, you never actually remove anything from a file. I assume you're writing the Vector's elements to a file somewhere else in your code. Removing an element from the Vector itself will not remove it from the file; you must rewrite the file.

If you're saving the Vector as a serialized object and attempting to view it in notepad, you still need to rewrite the file with the new Vector object after removing the desired element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM