我希望能够使用字符串调用某个 int。比如
final int music = 0;
final int film = 1;
int[][] matrix = new int[2][2];
假设我有一个包含“音乐”的字符串 m 和一个包含“电影”的字符串 f
有什么方法可以让它工作:
matrix[m][f] = 1;
最佳答案
一种方法是使用 HashMap:
HashMap<String,Integer> matrix = new HashMap<String,Integer>();
matrix.put("music", 1);
matrix.put("film", 2);
String m = "music";
System.out.println(matrix.get(m));
关于java - 是否可以将字符串链接到 int?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20374057/