Dear All:
Simple program, creates enum with breakfast theme. Then, at random, displays an item.
Why do I want this program to do this?
To understand. No real programming application at this juncture.
Question
Why is the statement
The way it is?BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)];
I believe that we are creating an object called "breaky", of the class "BreakfastItem".
I am not sure what value ".Values()" is getting, and I have no idea why the square brackets "[]" are there.
I AM NOT INTERESTED IN.
I have some understanding that I am dealing with the .ordinal() value. I know that the ordinal value exists and it corresponds to an enum object of the class "BreakfastItem" Please do not concentrate on the fact that the method ".ordinal()" exists.
So I understand what the outcome of the line is I am more interested in the STRUCTURE and the why of the structure.
FOLLOW UP QUESTIONS
Why is the statement constructed in the way it is? (I don't understand, on the Right hand side, anything after "BreakfastItem.")
What is with the [], the square brackets are reminiscent of arrays. I have some small knowledge of Arrays.
FULL CODE OF PROGRAM
public class BreakChoice { enum BreakfastItem { Eggs, Sauseges, Toast, Pancakes, Tater, Totts, French, Porrage } public static void main(String[] args) { Random r = new Random(); BreakfastItem breaky = BreakfastItem.values()[ r.nextInt(8)]; // LOOK HERE , LOOK HERE :-h System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky))); breaky = BreakfastItem.values()[ r.nextInt(8)]; System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky))); breaky = BreakfastItem.values()[ r.nextInt(8)]; System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky))); breaky = BreakfastItem.values()[ r.nextInt(8)]; System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky))); breaky = BreakfastItem.values()[ r.nextInt(8)]; System.out.println((breaky == BreakfastItem.Tater ? "Tater Totts": (breaky == BreakfastItem.French ? "French Toast" : breaky))); breaky = BreakfastItem.values()[ r.nextInt(8)]; }}
Thanks all for your help.
SPACE MONKEY