Creating an n-dimension Array in Java during runtime

前端 未结 2 691
清酒与你
清酒与你 2021-01-07 06:38

I have a String which contains arrays. Example:

\"[[1, 2], [4, 5], [7, 8]]\"

Now, I want to make an actual Java array out of this. I have c

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 07:00

    If you are doing numeric work then you should probably use a library. For example my open source library Vectorz provides proper support for multidimensional arrays / matrices (with doubles).

    Then you can just do something like:

    INDArray m=Arrayz.parse("[[1, 2], [4, 5], [7, 8]]");
    

    The INDArray object encapsulates all the multidimensionality so you don't need to worry about writing big nested loops all the time, plus it provides a lot of other functions and capabilities that normal Java arrays don't have.

提交回复
热议问题