Search code examples
javaarraysrecursionhashmapdimensions

HashMap n-dimensional using a recursive function


I don't know how to make a matrix n dimensional using HashMap. The idea is to enter a number which indicates the dimension of the matrix and from this make a recursive function which creates the n-dimensional HashMap.

For example, if you want a 5 dimensional matrix, you have to do inside of it one with 4 dimensions, inside 3 dimensions and etc..


Solution

  • Map<List<Integer>, ValueType>
    

    You can just fill your Lists that are used for the key of the map with N amount of numbers. Each number in your list specifies an offset of the dimension specified by the index of the number in your list. There is not much more to say, you can of course wrap it around and defend from "out of bounds" and such indefinite operations, which you determine.