9

Is it possible to create in Java an array indexed by letter characters ('a' to 'z') rather than by integers?

With such an array "a", I would like to useit in this way, for example

print (a['a']);

9 Answers 9

12

Is it possible to create in Java an array indexed by letter characters ('a' to 'z') rather than by integers?

Of course it is possible.
You could do this either like this:

char theChar = 'x';  
print (a[theChar - 'a']);   

or assuming handling only ASCII strings just declare the array of size 256. The directly index the array using your character.

char[] a = new char[256];   
char theChar = 'x';  
print (a[theChar]);    

Now you don't care if it is uppercase/lower case or whatever.
Actually if you are interested specifically for ASCII strings using a Map could be overkill compared to a simple array. The array doesn't waste so much space and perhaps a Map (a very efficient construct) is too much for such a simple task.

Sign up to request clarification or add additional context in comments.

1 Comment

Why 256 for ASCII and not 128? ASCII is a 7-bit code, representing 128 different characters; just my thought.
6

Use a Map instead.

Map<Character, Object> myMap = new HashMap<Character, Object>();
myMap.put('a', something);

print(myMap.get('a'));

On the other hand, as others already suggested, you can use a char as index (but you would leave all array elements 0...'a'-1 empty):

String[] a = new String['z' + 1];
a['a'] = "Hello World";
System.out.println(a['a']);

3 Comments

new String['z' + 1] creates an array of size 123. Better to declare it as new String[128] to handle basic ASCII or new String[256] to completely handle ASCII characters.
Why? I wouldn't allocate more than necessary.
For clarity.Shows your intentions better and the extra space is trivial
3

You could create an array of 26 elements and always substract 'a' from you char index:

int[] array = new int[26];
array['a'-'a']=0;
array['b'-'a']=1;
\\ etc...

Comments

2

What about something simple like this?

public static int getLetterValue(char letter) {
    return (int) Character.toUpperCase(letter) - 64;
}

and use it like so:

System.out.println(a[getLetterValue('a'));

This will fail pretty hard as it stand at the moment. You will need to check it's within range etc.

Alternatively you could implement the Java List interface and override the .get and .add methods so that they can use chars. But that brings me to my next point.

It's better to use a data structure that handles exceptions better, and is designed for that sort of use case. A Map is a much better choice.

Comments

2

Yes and no. Yes because you can do it and it will compile. Try the following code:

class foo {
    public static void main(String[] args) throws Exception {
        int a[] = new int[100];
        a['a'] = '1';
        System.out.printf("%d\n", a['a']);
    }
}

No, because the chars will be implicitly converted to ints, which doesn't sound like what you're looking for.

Comments

0

The data structure you are looking for is called Map in Java land.

This data structure is known by various names, such as associative array in PHP; dictionary in C#, Python; hash in Ruby etc which leads to this kind of confusion.

Comments

0

You could do something like this:- for eg:

char[] a = new char[]{'s','t'};
int[] result = new int[256];
result[a[0]]= 100;
System.out.println(result['s']);//will print 100

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
-1

No, You cannot do that. In the situation you should use Map instead.

Comments

-2

I think this is a duplicate question! See Can Java use String as an index array key? (ex: array["a"]=1;) .

You should use a map to map the letter to the value and call get to get the value.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.