Moe
Moe

Reputation: 119

Automatically removing object(s) of a removed object

I have three classes I am working with. 1)Category 2)MainCategory 3)SubCategory

  1. Category: responsible for creating/removing Main Categories
  2. MainCategory: respnsible for creating/removing Sub Categories (and nesting them under their respective main categories)
  3. SubCategory: Sets/gets the name of its own instances

The problem that I am having is that when I delete one of my Main Category objects (using .remove) the Sub Categories under it remain and get shifted to the next Main Category. I really want them to be deleted along in case their (Parent) category is has been removed. I point to the problem in the comments in my Main Method at the end of this post. Thank you.

## Category (Class) ##

public class Category {
private ArrayList<MainCategory> mainCat  = new ArrayList<MainCategory>();
private String name;


public void addMainCat (MainCategory name){
    mainCat.add(name);
}


public ArrayList<MainCategory> returnList() {
    return mainCat;
}



public void removeCat(int location){
    System.out.println("The Main Category " + mainCat.get(location).getName() + " has been deleted.");
    mainCat.remove(location);

}

public void removeCat(MainCategory name){
    mainCat.remove(name);
}

}

## MainCategory (Class) ##

public class MainCategory{
private ArrayList<SubCategory> subCat  = new ArrayList<SubCategory>();
private String name;


public void addSubCat(SubCategory name){
    subCat.add(name);
}

public void removeSubCat(SubCategory name){
    subCat.remove(name);
}


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public ArrayList<SubCategory> returnList(){
    return subCat;
}

}

## SubCategory (Class) ##

public class SubCategory{


private String name;


public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

}

## Main (Method) ##

    public static void main(String[] args) {

    Category stats = new Category();

    MainCategory mc1 = new MainCategory();
    MainCategory mc2 = new MainCategory();

    SubCategory sc1 = new SubCategory();
    SubCategory sc2 = new SubCategory();

    stats.addMainCat(mc1);
    stats.addMainCat(mc2);

    mc1.setName("salary");

    mc1.addSubCat(sc1);
    mc1.addSubCat(sc2);

    sc1.setName("mean");
    sc2.setName("median");

    System.out.println(stats.returnList().get(0).getName());
    System.out.println(mc1.returnList().get(0).getName());

    stats.removeCat(mc1); // This one is OK.

    System.out.println(stats.returnList().get(0).getName()); // Returns null, GOOD!
    System.out.println(mc1.returnList().get(0).getName()); // Returns mean (it should return null because mc1 was deleted)


}

Upvotes: 1

Views: 67

Answers (3)

zmo
zmo

Reputation: 24802

your code is not doing what you /want/ it to do, but what you /tell/ it to do. And you did not remove the category mc1, you removed the category referenced by mc1 from stats.

What that means is that in your code you created instances mc1 and stats:

Category stats = new Category();
MainCategory mc1 = new MainCategory();

then you've added mc1 to stats:

stats.addMainCat(mc1);

then you remove mc1 reference from stats:

stats.removeCat(mc1); // This one is OK.

which does:

mainCat.remove(location);

actually removing the reference location from mainCat list.

but as in the main() you still have a reference to mc1, it is still in your memory, and you can still manipulate it.

So you're not actually having any issue, mc1 is still the object you instanciated before adding it to stats, and will remain until you remove all references to the instance by doing mc1 = null;, and then at some point it will totally get destroyed thanks to the garbage collector.

Then, in your last two lines of the main(), your comment is wrong:

System.out.println(stats.returnList().get(0).getName()); // Returns null, GOOD!
System.out.println(mc1.returnList().get(0).getName()); // Returns mean (it should return null because mc1 was deleted)

the first one actually returns mc2's value, which happens to be null because you did not assign it a name. And then, the second one returns mean, because it is still the same mc1 object you're been working with within the main() function, whatever you did to stats.

The problem that I am having is that when I delete one of my Main Category objects (using .remove) the Sub Categories under it remain and get shifted to the next Main Category

So here you got it wrong. The sub categories does not get shifted to the next category, as long as mc1 is still being referenced, the subcategories will stay instanced along with the category instance. As soon as you dereference them (with e.g. mc1 = null) as you will not be able to look at them, they will disappear from the JVM's memory as well.


You should read more about how java works with instances and references to instances, to never wonder about this again!

cf:

as a few resources online to toughen up your knowledge of Java!

HTH

Upvotes: 2

PyThon
PyThon

Reputation: 1067

You have only removed the reference of main category from the top list, but main category is still an independent list containing more elements as sub list. you should manually removed them as

mc1.returnList().clear();

Upvotes: 1

matt_2006
matt_2006

Reputation: 31

You have a still reference to mc1 so it can`t be deleted. You call an instance which one you created in main class so you deleted only a reference from list not whole object in Category class.

Upvotes: 1

Related Questions