Bag is buggy. When I plugged it into an existing game instead of the ArrayLists I was using, it stopped working. All I used it for was the following: add elements to the Bag, iterate over the elements in the Bag, and sometimes remove an element using the iterator. I verified that it was after I removed the an element that the game started crashing, but I donāt see what the problem is. I did not create a minimal test case where this occurs, so I donāt know if youāll be able to reproduce the bug.
The clear method should be changed to check for the condition where the Bag is empty. Otherwise, calling the clear method on an empty Bag causes an IllegalArgumentException in the Arrays call.
What goodās the indexOf method? Thereās no get method to use the index. I think itās best that it doesnāt have a get method because this forces the person using the Bag class to use an Iterator. Using an Iterator preserves the conception that the Bag is a bag of unordered elements, not an array.
I did write one additional method: getRandomElement(). I believe this is a useful method that preserves the Bag concept, unlike a general get method.
I attached my version of Bag, which is basically the same as the previous except for the bug fix and the additional method. I also sorted the methods alphabetically and put the inner class and the variables at the bottom (since theyāre private anyways).
Seeing this Bag class suggests to me that we could write our own public domain classes that anyone on the Java Games Forum could modify. This would result in some nice open source code. When mixing the Bag class with my own code, I stored it in the jgf.util package. The ājgfā package could be the standard package for code from the Java Games Forum if we ever started writing public domain classes with any frequency.