2015-10-05 51 views
0

我知道这有一个答案,但我不知道要搜索什么,也不记得正确的措辞。Android - 是否有可能拥有查找数组?

基本上我的意思是,我被传递一个整数。我想用这个整数搜索一个数组来返回一个字符串值。

E.g.

我给出的数字2.我查找数组看起来像这样:

array.add(1, "Auckland") 
array.add(2, "Wellington") 
array.add(3, "Bay of Plenty") 

当我通过数组迭代,我想回到“惠灵顿”。

有人可以指点我正确的方向吗? :)

+1

是你的号码总是喜欢顺序那?如果是这样,为什么不按索引查找?如果这些数字是唯一的,可以尝试一下[Hashmap](https://stackoverflow.com/questions/11048783/java-hashmap-key-value-storage-and-retrieval)? – JGreenwell

回答

2

嗨,你应该申报整数字符串的一个HashMap:

HashMap<Integer, String> map= new HashMap<Integer, String>(); 

map.put(1, "Auckland"); 
map.put(2, "Wellington"); 
map.put(3, "Bay of Plenty"); 

然后得到的字符串,你应该使用键调用的HashMap:

String yourString = map.get(1); 
+0

谢谢!这正是我一直在寻找的:) – Noodelz

相关问题