0

i am trying to find a way to populate an array or some other data structure like the EnumSet with multiple object from that enum class. For example i have an enum class what contains for values like MONDAY, TUESDAY, WEDNESDAY, THURSDAY. What i want to do it to find a way to populate the data structure with the those values multiple times.

Array or Enumset with [MONDAY, MONDAY, MONDAY, MONDAY, TUESDAY, TUESDAY, TUESDAY .....]

I have use the EnumSet already but i've been only able to fill it with each one of them.

1
  • 1
    Just use an ArrayList. Commented Nov 10, 2013 at 14:40

1 Answer 1

1

Enum constants are just like regular static final variables referencing an enum instances. So you would create an array or a list just like with any other variable:

Day[] days = new Day[] {Day.MONDAY, Day.MONDAY, ...}

or

List<Day> days = new ArrayList<>();
days.add(Day.MONDAY);
days.add(Day.MONDAY); 
...

The reason you can't have duplicates in an EnumSet is that a Set, by definition, is a collection that prevents duplicates.

Sign up to request clarification or add additional context in comments.

2 Comments

I thought to use something similar like but i was afraid it wouldn't work. Another small question, say my enum has it's values like MONDAY(1) where 1 is a integer and it also has a constructor to initialize that value. When i'll make the list, will this value still be accessible? I've never used enumerations that much before so i am a bit lost..
An enum instance is an object, like all the other Java objects. Your list will contain these enum instances. So if your enum has, for example, a getIntValue() method which returns this int value, then you can call it to get this int value. Google for "Java enum tutorial", and read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.