我要定义很多固定值数组,
比方说 其中一个叫学校数组 01是小学 02是初中 03是高中 04是大学
另外还有很多这种数组 比方说公司类别 01是国企 02是私企之类的然后我想写一个函数 验证某个id值在某个数组中是否存在isCodeExist(哪个数组,id是什么)
我该怎么定义这些个数组和这个函数呢?
比方说 其中一个叫学校数组 01是小学 02是初中 03是高中 04是大学
另外还有很多这种数组 比方说公司类别 01是国企 02是私企之类的然后我想写一个函数 验证某个id值在某个数组中是否存在isCodeExist(哪个数组,id是什么)
我该怎么定义这些个数组和这个函数呢?
直接根据 map.containsKey(key)或者map.containsValue(value)判断
{
for (Object o : objs)
{
if (o == obj)
{
return true;
}
}
return false;
}
schoolMap.put("02","初中");
schoolMap.put("03","高中");
public boolean isCodeExist(TreeMap map,String id){
if(map.containsKey("01")){
return true;
}else{
return false;
}
}
谢谢。 TreeMap<String,String> schoolMap = new TreeMap<String,String>();schoolMap.put("01","小学");
schoolMap.put("02","初中");
schoolMap.put("03","高中");这个应该放在什么位置?
定义是可以 schoolMap.put("01","小学");
schoolMap.put("02","初中");
schoolMap.put("03","高中");这个呢?
schoolMap.put("01","小学");
schoolMap.put"02","初中");
}
public class CodeDictConstant { public static final String 有無_無_CD = "0";
public static final String 有無_有_CD = "1";
public static final String 有無_無 = "無";
public static final String 有無_有 = "有"; public static final TreeMap<String,String> 有無;
static{
有無 = new TreeMap<String, String>();
有無.put(有無_無_CD, 有無_無);
有無.put(有無_有_CD, 有無_有);
} public static boolean isCodeExist(TreeMap<String, String> map,String cd){ if(map.containsKey(cd)){
return true;
}else{
return false;
}
} public static void main(String args[]) { if (isCodeExist(有無,"0")){
System.out.println("0 is existed");
} if (isCodeExist(有無,"1")){
System.out.println("1 is existed");
} if (isCodeExist(有無,"2")){
System.out.println("2 is existed");
}else{
System.out.println("2 is not existed");
} }
}
好像可以了
0 is existed
1 is existed
2 is not existed
完善下很有必要,hehe
谢谢。。可惜我不知道怎么完善。。能否具体点我第一次接触java.....
另外 抱歉 csdn制允许我加到100分
如果是我做的话,就把固定变量单独放一个类:public class CodeConst{
public static TreeMap<String,String>() schoolMap = new TreeMap<String,String>(); public static TreeMap<String,String>() companyMap = new TreeMap<String,String>(); static{
schoolMap.put("01","小学");
schoolMap.put("02","初中"); companyMap.put("01","国企");
companyMap.put("02","私企");
} public static boolean isCodeExist(TreeMap<String, String> map,String cd){ if(map!=null && map.containsKey(cd)){
return true;
}else{
return false;
}
}
}
public static final String 有無_無_CD = "0";
public static final String 有無_有_CD = "1";
public static final String 有無_無 = "無";
public static final String 有無_有 = "有";放到其他类里面?