Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm just learning out Java and making a small console-game.

I have a Board class, which is done with 2-dimensional (Map) object array. Map represents "land" where I can have other objects on (Items, Monsters and so on..). Those objects are represented as ArrayLists in Map class (private instance variables). Map class has removeFromMap method, which removes a record from parameter specified ArrayList.

My Map instance variable:

private List<Monster> monsters = new ArrayList<Monster>();

My removeFromMap method:

public void removeFromMap(Monster monster) {
        this.monsters.remove(monster);
}

The problem is here, that if I'm going to have 100 objects, which can be on that "land" (Map), I will have 100 removeFromMap methods.

How can I make it more abstract, so I can just have one remove method to remove multiple of different objects?

Something like this:

public void removeFromMap(Object object) {
        if(object_type_of_Monster) // _type_of_ is not a real function of course, but is there something like this in Java?
            this.monsters.remove((Monster)object);
        else if(object_type_of_Item)
            this.monsters.remove((Item)object);
        else
            // cannot remove this object from Map
}

If someone has a better idea - let me know!

share|improve this question

closed as off-topic by Brian Reichle, Uri Agassi, rolfl, syb0rg, Jamal Feb 27 at 22:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – Uri Agassi, syb0rg, Jamal
If this question can be reworded to fit the rules in the help center, please edit the question.

    
This question should be on Stack Overflow I think. There it would be better than on code review. –  Uwe Plonus Jan 11 '13 at 15:43

2 Answers 2

up vote 1 down vote accepted

Try to create a class ObjectOnMap which is the base class for Monster, Item etc.

Then you can write one method public void removeFromMap(ObjectOnMap object).

Your list is then List<ObjectOnMap>.

HTH

share|improve this answer
    
Thank you for your answer, it's exactly what I need. –  Furlando Jan 11 '13 at 15:54

I would suggest to put it on stackoverflow, because code review is for reviewing existing code.

Anyway, the thing you search for is inheritance and superclass. Take a look at Java tutorials or any book/tutorial about object programming fundamentals.

In this case, it could probably be an IngameObject superclass.

share|improve this answer
    
Yeah, I'm not very good at thinking in OOP yet. Yep, this superclass is exactly what I needed. –  Furlando Jan 11 '13 at 15:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.