0

i want to create a set of maps with dynamic names in java according to the size of arraylist. this is the code

for (int i = 0; i <array.size(); i++) { String name = "a_"+i; Map<Point, Double> name = new HashMap <Point, Double>(); }


in the above code i want to create a number of maps according to the size of array. i need the map name to be like a_0, a_1, a_2 .... so i can retrieve them and assign values to them later

prasad
  • 1,277
  • 2
  • 16
  • 39
Zain
  • 11
  • 3

2 Answers2

3

Dynamic variable names are directly not possible (as far as I know).
You could use a Map<String,Map<Point, Double>> to link the names to their specific map.

Map<String,Map<Point, Double>> nestedMap = new HashMap<>();
for (int i = 0; i <array.size(); i++)
{
        String name = "a_"+i;
        Map<Point, Double> map = new HashMap<Point, Double>();
        nestedMap.put(name, map);
} 
Turamarth
  • 2,282
  • 4
  • 25
  • 31
-1

As of my understanding it's not possible to create dynamic variables, instead of that you can create list and add to that list

List<Map> listMap = new ArrayList<>();
for (int i = 0; i <array.size(); i++) {
        Map<Point, Double> name = new HashMap <Point, Double>();
        listMap.add(i, name);
}
prasad
  • 1,277
  • 2
  • 16
  • 39