Unsure as to what type of collection I should be using.
My requirements
The collection will be constantly changing in size, adding to and removing objects.
Order does not matter
Length DOES matter, will have maximum length (3 or4) and must be able to retrieve length.
Sounds mostly like a straightforward List<>. I donât think List<> can enforce a maximum size, but you can do that in your code. After all, what do you want to happen if the your application attempts to exceed the maximum size? Although, if you set the capacity property of your list to maximum size, than you can keep adding and removing items without your list allocating memory, as long as you donât attempt to go over the maximum.
If you really want to, it would probably be pretty easy to implement your own collection, using an array (since your requirements are pretty simple).
As an alternative to List, your requirements sound almost like a âHashSetâ as well:
A HashSet is an unordered set of objects with no duplicates.
It has length.
It allows adding and removing.
Order is not conserved (you said no order necessary)
List will allow duplicates.
You didnât say if you wanted to allow duplicates or not. But if you donât want duplicates, with List youâll have to continually check âContainsâ at first.
HashSet is also an O(1) collection for finding entries (Contains, Remove). Though technically speaking at sizes as low as 3 or 4 in length the cost of hashing the object will make both List and HashSet perform similarly. Youâll only notice that O(1) gains when the collection grows larger.
âŠ
Personally I use HashSetâs a lot. For example I use them when I want to keep track of what is in/out of some situation.
Like for example if I have a Collider trigger and on enter I want to store that something is in the collider, and on exit I want to remove it from that store. Thusly giving me an easy access to what is currently intersecting the collider. I use a HashSet. This is especially useful since an entity might contain multiple colliders that trigger onenter repeatedly for the same entity. The non-duplicates quickly deals with that.
Like here:
Hereâs another:
This watches for if multiple t_'s are triggered, and triggers if all of them are. So like it can monitor multiple t_OnTriggerOccupied and signal if all of them are signaling.
This could be useful for those puzzles where each switch needs to be stepped on at the same time. You can monitor all N switches for being occupied and only signal when they all do. So then you can do a push puzzle to place boxes on them, or a multi-player puzzle where every player has to stand on it.
The usual thing is to start with a List. Those are arrays with useful functions built-in. Then only use some other container if thereâs a problem with speed in a large list:
o ArrayList is old Java-style. Never use it for anything.
o If you insert/remove lots of times not to the back, LinkedLists are faster.
o If the size is small, like 20 or less, it wonât even matter. Use a List, since thinking about using anything else is a distraction.
o Hashes/Sets are for when if(A[âhammerâ]!=null) is all you will ever need to do. When your container represents a set in math.
o If youâre using a system whoâs API takes and returns lots of arrays, such as Unity, and your container interacts with them, consider using an array.
So funny. Thatâs from StackExchange Java under âWhat collection should I useâ. Itâs copied from some guyâs blog page, which is now a 404. Of the three comments, one points out LinkedList is missing (which is pretty important). The other two are asking some technical Q and the guy responding âBeats me. The arrows to get to those 2 things have different words, is all I knowâ.
The other replies (to the Q, not the picture) explain them in words. One has the correct answer: read any book about how basic data structures are made. If you know how a LinkedHashMap works, youâll know when to use it (itâs a LinkedList and a Dictionary rammed together).
But the super funny part is we used to use flow charts in the '70, back when all we had was gotoâs. We quit right after that. Most of the flow charts you see now are jokes. I was thinking this was, and Iâm still not sure.
Not possible. I still need some concrete container type to create it, and Iâm stuck on the first diamond: does a flow chart contain key/value pairs, or values only?