I am using an ArrayList as my \"inventory\". I am having trouble figuring out a way to add multiples of the same item without taking up a spot in the \"inventory\". For exam
Similar to aioobe's solution, you can use TObjectIntHashMap.
TObjectIntHashMap- bag = new TObjectIntHashMap
- ();
// to add `toAdd`
bag.adjustOrPutValue(item, toAdd, toAdd);
// to get the count.
int count = bag.get(item);
// to remove some
int count = bag.get(item);
if (count < toRemove) throw new IllegalStateException();
bag.adjustValue(item, -toRemove);
// to removeAll
int count = bag.remove(item);
You can create a multiples class.
class MultipleOf {
int count;
final T t;
}
List bag = new ArrayList();
bag.add(new Sword());
bag.add(new MultipleOf(5, new Potion());
Or you can use a collection which records multiples by count.
e.g. a Bag
Bag bag = new HashBag() or TreeBag();
bag.add(new Sword());
bag.add(new Potion(), 5);
int count = bag.getCount(new Potion());