In Java, I have a Class called Couple which has a String and an int as istance variables. And i have an ArrayList which holds istances of Class Couple. A method foo, adds new couples to the ArrayList cop_list. But also, add every message to another ArrayList, called msg_list.
foo(String msg,int id)
{
Couple cop = new Couple(msg, id);
//ArrayList<Couple>
cop_list.add(cop);
//ArrayList<String>
msg_list.add(msg);
...
}
After that, When i call a delete(id) method, it should search and remove a couple by its id, but it should also remove the message from msg_list. So, my question is, when i delete a Couple object from the cop_list, what happens to the message in msg_list? msg_list still points to it until i explicitly remove it? String object msg is still on the heap?
delete(int id)
{
//search and find the couple, save its msg in a variable
msg = cop.getMsg();
cop_list.remove(cop);
//at this point, can/should i remove msg from msg_list?
//what happens if i call:
msglist.remove(msg);
}