0

Sorry for the title maybe this may seem like a dummy question. I'm new in java and assume I have a char array like this:

char[] hex = {
            '0', '1', '2', '3', '4', '5', '6', '7',
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };

Now I want to find an item's index on that array for example 'a', but after some research I realized java's array class doesn't have an indexOf method. So I did the following:

int index = new String(hex).indexOf('a');

And it works fine, and after that I try following code:

int index2 = Arrays.asList(hex).indexOf('a');

It doesn't work and after I saw this question I understood the reason. But now I'm curious why finding an index needs too much effort in Java and why we can't get index directly? Why java's Array class doesn't have an indexOf method? Or is there an other simple way to do this and I'm missing it?

Community
  • 1
  • 1
Selman Genç
  • 100,147
  • 13
  • 119
  • 184

2 Answers2

5

You have a few options...

  • You could use a List in the first place, which provides an indexOf() method.

  • You could keep the array sorted and use Arrays.binarySearch() (although perhaps this is overkill).

  • You could (fairly easily) write your own function for this:

    public static int indexOf(char[] array, char key) {
        for (int i = 0; i < array.length; i++)
            if (array[i] == key)
                return i;
    
        return -1;
    }
    
  • You could use a third-party library. Guava, for example, has Chars.indexOf(). Apache has ArrayUtils.indexOf() (which apparently deals better with null inputs).


For your specific array, since it has some nice properties, you could do this:

public static int indexOf(char key) {
    if ('0' <= key && key <= '8')
        return key - '0';

    if ('a' <= key && key <= 'f')
        return key - 'a' + 10;

    return -1;
}
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • thanks for the answer,I know I can write my own methods, and this is the same logic of string.indexOf method =) I'm just curious why java's creaters didn't add a simple method like this? – Selman Genç Jan 03 '14 at 22:03
  • @Selman22 You'll have to ask them, I'm afraid. There are many things that would be nice in Java but don't exist in the standard classes. That's why external libraries like Guava/Apache are very useful. – arshajii Jan 03 '14 at 22:04
  • @Selman22 May you want to look at Apache's [ArrayUtils class](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/ArrayUtils.html), specially the method `int indexOf(char[] array, char valueToFind)` – Alexis C. Jan 03 '14 at 22:06
  • @arshajii I prefer the method from apache instead of `Chars.indexOf()`, since it deals better with a potential `null` array as input. – Alexis C. Jan 03 '14 at 22:10
  • @ZouZou thanks it seems fine but may I ask how can I import this library into my project ? I tried `import java.lang.Object` but it doesn't work I guess i need to download it but i couldn't find a download link – Selman Genç Jan 03 '14 at 22:15
  • @Selman22 This is an external library, not part of the JDK. So you'll have to download it. – Alexis C. Jan 03 '14 at 22:18
  • @ZouZou okey,I found now it was here [link](http://commons.apache.org/proper/commons-lang/download_lang.cgi) – Selman Genç Jan 03 '14 at 22:19
3

The problem with Arrays.asList() is that it converts your primitives to objects (boxing) which may be expensive and memory inefficient.

If your array is not sorted, just write the indexOf method yourself:

public int indexOf(char[] hex, char value){
    for(int i=0; i< hex.length; i++){
       if(hex[i] == value){
         return i;
       }
    }
    return -1;
}
dkatzel
  • 31,188
  • 3
  • 63
  • 67