问题描述:

I have my application design where I have a set of cities and I want to add them in a Enum. Mostly for a year or so those are the cities I will need to work on.

However the city list will grow sooner or later, which will then require to add elements to enum.

From what I understand there is no 'nice' way to add elements to an Enum, there seen to be certain hacks but for now it looks as if it is better to steer away from them.

So what should be my approach in this case? I could use plain String but I would want something better.

网友答案:

Why not just use a Set for this. And use the set's iterator to enumerate over all the cities.

Enum is not designed to be dynamically updated. its conceptually static in nature

网友答案:

you could probably use a flyweight approach (if my memory serves me correctly) whereas you have a class with a private constructor (similar to Singleton) and this class will manage all the instances of itself -- there will be one single instance in the system for each city, all managed by the class itself, and as such you can use == instead of equals() just as you would with enum.

相关阅读:
Top