Java: create an array with letter characters as an index

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

With such an array "a", I would like to use it this way, for example

print (a['a']); 
+4
source share
9 answers

Use Map instead.

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

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

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

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

Of course it is possible.
You can do it like this:

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

or assuming that processing only ASCII strings just declares an array of size 256. 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 / lowercase letters or something else.
In fact, if you are interested specifically for ASCII strings , using Map can be superfluous compared to a simple array. The array does not waste so much space and perhaps Map (a very efficient construction) is too much for such a simple task.

+4
source

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

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

How about something simple?

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

and use it like this:

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

It will be very difficult, as it stands at the moment. You will need to check it within range, etc.

Alternatively, you can implement the Java list interface and override the .get and .add methods so that they can use characters. But that brings me to the next point.

It is better to use a data structure that handles exceptions better and is designed for this kind of use. A Map is a much better choice.

+2
source

Yes and no. Yes, because you can do this and compile it. 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 characters will be implicitly converted to ints, which is not what you are looking for.

+1
source

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

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

0
source

You can do something like this: - for example:

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

No, you cannot do this. In this situation you should use Map .

-1
source

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

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

-2
source

All Articles