所以我有这4个变量
private final String PROG_DEPT = "PROGRAMMING/ENGINEERING";
private final String DES_DEPT = "DESIGN/WRITING";
private final String ART_DEPT = "VISUAL ARTS";
private final String SOUND_DEPT = "AUDIO";
我想要做的是获取一个字符串并将其与变量进行比较,然后输出变量包含的内容(如果它等于它)。
例如,如果我的字符串等于“ART_DEPT”,则检查是否存在名为ART_DEPT的变量,然后输出“VISUAL ARTS”
我想把它放在2D字符串数组或列表中,但我不确定如何做我想做的事
答案 0 :(得分:8)
您要查找的数据类型为Map<String, String>
。
Map<String, String> departmentNames = new HashMap<String, String>();
departmentNames.put("PROG_DEPT", "PROGRAMMING/ENGINEERING");
departmentNames.put("DES_DEPT", "DESIGN/WRITING");
//...etc...
//...
String dept = "PROG_DEPT";
String deptName = departmentNames.get(dept);
System.out.println(deptName); //outputs "PROGRAMMING/ENGINEERING"
Map
将唯一键绑定到值。在这种情况下,两者都具有类型String
。您可以使用put(key, value)
添加绑定,并使用get(key)
获取密钥的绑定。
答案 1 :(得分:5)
我会选择枚举:
package com.stackoverflow.so18327373;
public class App {
public static void main(final String[] args) {
final String in = "DES_DEPT";
try {
final Departement departement = Departement.valueOf(in);
System.out.println(departement.getLabel());
} catch (final IllegalArgumentException ex) {
// in was not a known departement
System.err.println("Bad value: " + in);
}
}
public static enum Departement {
PROG_DEPT("PROGRAMMING/ENGINEERING"),
DES_DEPT("DESIGN/WRITING"),
ART_DEPT("VISUAL ARTS"),
SOUND_DEPT("AUDIO");
private final String label;
private Departement(final String label) {
this.label = label;
}
public String getLabel() {
return this.label;
}
}
}
然后使用valueOf()
答案 2 :(得分:4)
您可能想要使用某种Map
,例如HashMap<String,String>
。我建议您阅读Map
接口和HashMap
类的Javadoc。
答案 3 :(得分:3)
您需要使用的是地图。
private final Map<String,String> myMap= new HashMap<String,String>() ;
{
myMap.put("PROG_DEPT","PROGRAMMING/ENGINEERING");
myMap.put("DES_DEPT","DESIGN/WRITING");
myMap.put("ART_DEPT","VISUAL ARTS");
myMap.put("SOUND_DEPT","AUDIO");
}
然后按以下方式使用它:
String input= "ART_DEPT" ;
System.out.println( myMap.get(input) );
答案 4 :(得分:1)
听起来好像在寻找反射(或者如果你想使用不同的数据类型而不是在类中查找变量,那么Map<String, String>
)。看起来Map方法很好,所以只是因为这对我来说很有趣,这里是反射方法(并不是这不是解决这个问题的最佳方法,而是因为你要求检查一个变量是否存在然后得到它的价值)
import java.lang.reflect.Field;
public class SOQuestion {
private final String PROG_DEPT = "PROGRAMMING/ENGINEERING";
private final String DES_DEPT = "DESIGN/WRITING";
private final String ART_DEPT = "VISUAL ARTS";
private final String SOUND_DEPT = "AUDIO";
public static void main(String ... args) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
System.out.println(reflectValue("ART_DEPT", SOQuestion.class));
System.out.println(reflectValue("COMP_DEPT", SOQuestion.class));
}
public static String reflectValue(String varible, Class thing) throws IllegalArgumentException, IllegalAccessException, InstantiationException {
Field[] fs = thing.getDeclaredFields();
for(int i = 0; i < fs.length; i++) {
if(fs[i].getName().equals(varible)) {
fs[i].setAccessible(true);
return (String) fs[i].get(thing.newInstance());
}
}
return null;
}
}
第一个打印"ATR_DEPT"
的请求将打印VISUAL ARTS
,而对不存在的"COMP_DEPT"
的第二个请求将返回null;
答案 5 :(得分:1)
试试这个
List<String> list=new ArrayList<>();
list.add("private final String PROG_DEPT = \"PROGRAMMING/ENGINEERING\";");
list.add("private final String DES_DEPT = \"DESIGN/WRITING\";");
list.add("private final String ART_DEPT = \"VISUAL ARTS\";");
list.add("private final String SOUND_DEPT = \"AUDIO\";");
String search="ART_DEPT";
for (String i:list){
if(i.contains(search)){
System.out.println(i.split("=")[1].replaceAll(";",""));
}
}
在这里实时Demo。你可以使用Map来做到这一点,但要做到这一点,你必须从这些字符串创建一个地图。
答案 6 :(得分:1)
private String getStaticFieldValue(String fieldName){
String value = null;
try {
Field field = getClass().getDeclaredField(fieldName);
if (Modifier.isStatic(field.getModifiers())){
value = field.get(null).toString();
}
}
catch (Exception e) {
return null;
}
return value;
}
上面提到的选项很少:
答案 7 :(得分:0)
使用Map
的概念import java.util.HashMap;
import java.util.Map;
public class MajorMap {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<String, String> deptMap = new HashMap<String, String>();
deptMap.put("PROG_DEPT", "PROGRAMMING/ENGINEERING");
deptMap.put("DES_DEPT","DESIGN/WRITING");
deptMap.put("ART_DEPT","VISUAL ARTS");
deptMap.put("SOUND_DEPT","AUDIO");
System.out.println("ART_DEPT----->>"+deptMap.get("ART_DEPT"));
}
}