0

Using Java I am importing a CSV file which contains a list of topic types and questions. I have produced a list of the topics (in my test I have 10 topics and 29 questions).

I would like to create a series of lists for each topic, but instead of typing the list names directly into the source code which I currently have I would like to use names generated from values in the CSV file.

//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<>();

2 Answers2

1

You cant name the variable by using other's variable String value, but may be for your purpouse would be usefull using HashMap

 List<ExamQuestion> listName = new ArrayList<>();
 Map<String, List<ExamQuestion>> map = new HashMap<>();
 map.put("name", new ArrayList<>());
Ludov Dmitrii
  • 425
  • 5
  • 9
0

In Java, it is not possible to use Strings as variable names. What you can do, however, is create a Map<String, List<ExampleQuestion>> and store to a List<...> to a given String (its "name").

Take a look at @Ludov's answer tor a sketch implementation utilizing HashMap<...>.

Community
  • 1
  • 1
Turing85
  • 18,217
  • 7
  • 33
  • 58