Paradoxical initializers [Answered]
Two classes. Class Character and Class Guild.
Character has a field of Guild.
Guild has an array of Members composed of a Character[] array.
Wondering how to re-concile the constructors to allow for creation of one before the other.
Code attached:
9 Replies
how about you use a collection (such as a list) of characters for the guild?
then just create the guild with an empty list (== no members), and add to it as needed
you can technically do the same by simply replacing the array object as needed but it's not as clean as using collections that were designed with mutability in mind
So I can't pass it an empty object of Characters? I need to switch from object of Char to List of Char?
yeah, generally you don't use arrays unless you know exactly how many elements are going to be in there and have no need for dynamic changes, it's a lot harder to manage moving/removing entities from an array manually
generally a
List
or HashSet
(if you need quick lookups and guaranteed uniqueness) is better for dynamic collections
an array would make more sense if you needed two things, a guaranteed limit on the number of members (which doesn't really need an array anyway) and some strict positioning rules (where a character is in the array actually means something)
but you can do both without an array anyway, so there's no real needAlright, I'm just getting started. But I assume the keyword I should be researching is List<Collection>?
That was simple. Thanks a bunch for the help.
Well you don't want a list of collections, do you
Right. I just remember that's how it was written for Java collections.
OQ is resolved. Do I mark this thread as complete or just let it sit and eventually be forgotten?
Also no. It's the exact same in java.
List<ThingInList>.
If you make a List<Collection> that's a list of collections (of other things..)
/close
to the rescue
✅ This post has been marked as answered!