views:

49

answers:

1

I have this array of strings

private static String[] colorsArray = { "#bde876", "#ff8581", "#ffc472",
    "#faed75", "#a8c9e5", "#999999", "#e3a8e5", "#dddddd", "#fc603c",
    "#ffcc00", "#74e8d4", "#3cd6fc" };

Then I have this method

public static int getColorByString(String color) {
    return Arrays.binarySearch(colorsArray, color);
}

When I call getColorByString("#ff8581"); it gives me -13 as result.

If I understood well, it means that the element is not contained in my array.

What am I doing wrong? How can I make it work?

EDIT

I just realized the array has to be sorted. The problem is I can't sort it, because I need to map the strings to a specific index.

So now the question becomes, is there any method that performs a linear search or do I have to write it?

+1  A: 

How about

Arrays.<String>asList(colorArray).indexOf("#ff8581");
tim_wonil
Yes my friend! Thanks a lot!
klez
While you are at it, you might consider making your `String[]` into a `List<String>`. This gives you access to more methods easily, and prevents the overhead of creating new `List` objects every time you want to use one of those methods.
Mike
I'll do, thanks Mike
klez