我正在使用 Java 导入一个 CSV 文件,其中包含主题类型和问题的列表。我已经制作了一个主题列表(在我的测试中,我有 10 个主题和 29 个问题)。
我想为每个主题创建一系列列表,但我不想将列表名称直接输入到我当前拥有的源代码中,而是想使用从 CSV 文件中的值生成的名称。
//current code (ExamQuestion is a custom class)
List<ExamQuestion> hardwareList = new ArrayList<>();
//desired code (in real version variable value would come from CSV file)
String listName = "hardwareList";
List<ExamQuestion> listName = new ArrayList<>();
最佳答案
您不能使用其他变量的字符串值来命名变量,但对于您的目的,使用 HashMap 可能会很有用
List<ExamQuestion> listName = new ArrayList<>();
Map<String, List<ExamQuestion>> map = new HashMap<>();
map.put("name", new ArrayList<>());
关于java - 有没有办法使用变量来命名列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57231976/