Java:创建一个以字母为索引的数组 [英] Java: Create an array with letter characters as index
本文介绍了Java:创建一个以字母为索引的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在Java中创建一个由字母字符('a'到'z')而不是整数编制索引的数组?
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']);
推荐答案
使用地图
代替。
Map<Character, Object> myMap = new HashMap<Character, Object>();
myMap.put('a', something);
print(myMap.get('a'));
另一方面,正如其他人已经建议的那样,你可以使用char作为索引(但你会保留所有数组元素 0 ...'a'-1
为空):
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']);
这篇关于Java:创建一个以字母为索引的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文