How to check whether the variable name contains a string, and then output the string variable content

So I have these four variables

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";

What I want to do is take a string and compare it with the variable, and then output the contents of the variable (if it is equal to it)

For example, if my string is equal to "art_dept", it checks whether there is a string named art_ The variable of dept, and then output "visual arts"

I want to put it in a 2D string array or list, but I'm not sure how to do what I want to do

Solution

The data type you are looking for is 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 binds unique keys to values In this case, both have a string type Use put (key, value) to add the binding, and use get (key) to get the binding of the key

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>