Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Java
»
Beginning Java
Enum Issue
Max Bean
,
Ranch Hand
Aug 04, 2007 02:15:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Suppose I have
public enum Test { Love, Hate, Adore, Disgust };
Is there any method to count the number of this enum and also loop to get each member of this enum?
Thnx
Ilja Preuss
,
author
staff
Aug 04, 2007 04:32:00
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
If I remember correctly,
Test.values()
will give you an array of all
Test
instances, in the order they are declared.
Remko Strating
,
Ranch Hand
Aug 04, 2007 04:35:00
(0 cow)
(0 skulls)
1
+Pie
Number of slices to send:
Send
The following code solves your questions
public class Test { enum Season { WINTER, SPRING, SUMMER, FALL } ; public static void main ( String... args ) { Season[] season = Season.values(); System.out.println("Qty of elements in the enum Season " + season.length); for ( Season seasons : season ) { System.out.println ( seasons.toString ( ) ) ; } } }
Post by:
autobot
Think of how dumb the average person is. Mathematically, half of them are EVEN DUMBER. Smart tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
This thread has been viewed 1010 times.
Similar Threads
How to define constants for several classes?
enum parameter in method declaration
Can a enum class be dynmaically created
Enum inside an Enum
retrieve enumeration datatype into jsp
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:22:43
.