[英]How do I return an array by passing a string as reference to the array in Java?
I have array lists of cities defined by state abbreviations. 我有州名缩写定义的城市列表。
static List<String> AL = Arrays.asList("ABBEVILLE","ADAMSVILLE",.....
static List<String> AK = Arrays.asList("ADAK","AKHIOK",......
The same thing happens to the array no matter which one is called. 无论调用哪一个,数组都会发生同样的事情。 How do I pass a string of 'AL' and access the array list AL? 如何传递一个'AL'字符串并访问数组列表AL? I currently use a case statement... But its a lot of code for all 50 and I want to trim it down a bit.. 我目前使用的是一个案例陈述...但是它的所有50个代码都很多,我想把它修剪一下......
case "AL":
for(int index = 0; index < AL.size(); index++){.....}
case "AK":
for(int index = 0; index < AK.size(); index++){.....}
Id rather something like this: 我喜欢这样的东西:
for(int index = 0; index < state_abbreviation.size(){
System.out.println(state_abbreviation[index]);
You could store every List<String>
in a Map
and use the state abbreviation as the map's key. 您可以将每个List<String>
存储在Map
并使用州缩写作为地图的键。
import java.util.*;
class Test {
public static void main(String[] args) {
Map<String, List<String>> states = new HashMap<String, List<String>>();
List<String> al = Arrays.asList("ABBEVILLE", "ADAMSVILLE", "...");
List<String> ak = Arrays.asList("ADAK", "AKHIOK", "...");
states.put("AL", al);
states.put("AK", ak);
System.out.println(states.get("AL").get(1)); // ADAMSVILLE
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.