• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to code this fine: returning an ArrayList containing a part of an enum's members

 
Ranch Hand
Posts: 41
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers!

I've ran into a bit of a block where I dont want to solve a problem by chaining 10 or so of   myList.add(first).add(second)...add(thirteenth);    into a method, or even write them out on separate lines. I haven't been able to come up with any useful or not complicated ideas to solve this, so here is my enum:



The whole thing is because I store all of these in the same database table. For various complicated reasons, this has to stay like this. Thus, to separate the special work times that a vehicle has (in service, in rent, out of service) and the special work times that a person/user has (holiday, sickleave etc) I need these two static methods to be able to conviniently retrieve them.

My question is, how to do the methods more handsomely? Adding the enum members one-by-one is pretty unfriendly for future expansion of the enum.
My thought was to somehow make them into a loop, but I couldn't find an enum attribute that would let me to. Maybe make an individual ID for each, in their constructor? But then I would need an instance of each?
Making a private static int counter burnt in it would be risky of mistakes, too. I'm out of ideas!

Is there a common way to do this that I have overlooked?
I looked on stack overflow but couldn't find an example for this, please rescue my newbie soul!

Best regards and thank you for your time, Dóri
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think that a bunch of add() statements would be inelegant, but I did come up with a way to do this in one loop -- but it's a little complex.

It involves adding an argument to the enum, then creating a constructor that sets that argument to a field, then using that field in a static block to create two (or more) lists separated by the value of the field.

So, without giving too much away, you can create you enum like this:

...then write a constructor like this:

See if you can get the rest.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get an array of all elements of an enum with one of its methods, and use a method of the Arrays class (I think) to convert that to a List.
 
Dóra Takács
Ranch Hand
Posts: 41
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for the answers! I wasn't aware of the .values() method that enums have, and giving types was a great idea too. Although I have settled with a way of not-adding vehicle types from the array values() gives back, and only adding that few ones to the other one, I definitely learnt something from the idea, and I might use it in an other enum I have.

Thank you for your times!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic