Java: Create an array with letter characters as index

arrays, indexing, java

Solution

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']);

Problem

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']); ```

Original source

Related problems