0

I am trying to store the Color values in Java Array but I know that these values are stored as strings,

String[] colorarray = {"Color.yellow","Color.red","Color.blue"};

Now, I couldn't access this array element to set the color, i.e

g.setColor(colorarray[0]);

Because all the values in the array are strings. How could I convert these values to the constants ?

1
  • Maybe an enum would be a better fit? Oct 14, 2013 at 21:26

3 Answers 3

11

Use

Color[] colorArray = {Color.YELLOW, Color.RED, Color.BLUE};
4
  • 1
    I didn't downvote (and won't) but you do assume that he has control over the array Oct 14, 2013 at 21:28
  • Reflection is definitely something which I would try and steer away from especially given its performance cost/complexity vs. a simple array lookup
    – Reimeus
    Oct 14, 2013 at 21:44
  • @Reimus there are definitely pros and cons. The performance cost is only a con though if the application is getting fairly heavy use. The question boils down to weighing whether the extra code to translate from strings to Color.XXX via switch is going to be enough of a performance gain to justify cluttering your code. I'm guessing from the fact that he accepted my answer over yours that he doesn't have control of how the array is coming to him, though I don't really know Oct 14, 2013 at 21:47
  • For the record, assuming he DOES have control over the array, I agree with you that a Color[] array is a far better solution Oct 14, 2013 at 21:49
7

Straightforward, use this :

Color[] colorarray = {Color.YELLOW, Color.RED, Color.BLUE};
2

If you have control over the array, by all means change it to just be an array of Colors as suggested by several other people.

Color[] colorArray = {Color.YELLOW, Color.RED, Color.BLUE};

If you don't have control over the array and you have to use it as-is, you'll need to do one of two things:

  1. Use reflection to access the static members of the Color class (a quick Google search will give you plenty of examples on how to do this so I won't go into it here)
  2. Create a Java 7 switch statement on the Strings to interpret each possible color value in the array into Color.XXX where XXX is the name of your color. (This could also be done using a big if-else block but that wouldn't be as clean)

One other quick note - variable names in Java are, by convention, in camelCase starting with a lower-case letter. This helps make your code more readable. (Class names are CamelCase, package names are lower.case)

2
  • Note that the switch-on-strings solution is pretty cumbersome, which is why I listed it second. Reflection would be better. Oct 14, 2013 at 21:38
  • Correction: It would be better assuming the performance overhead of using reflection isn't an obstacle for your application. That's something only you can weigh Oct 14, 2013 at 21:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.