创建运行在Java中的n维数组 [英] Creating an n-dimension Array in Java during runtime

查看:530
本文介绍了创建运行在Java中的n维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数组的String。例如:

I have a String which contains arrays. Example:

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

现在,我想打一个实际的Java数组出于此。我创建了一个函数来获得尺寸和递归函数来获取数组的每一个元素。因此,我有充分的1-D阵列创建的,但我想知道是否有在Java中的一种方式来创建我的运行过程中发现的维数组?维度返回作为数组。等,用于上述例子的尺寸是:

Now, I want to make an actual Java array out of this. I have created a function to get the dimensions and a recursive function to get each and every element of the array. Thus, I have every 1-D array created, but I would like to know if there is a way in Java to create an array of the dimension that I found during the runtime? The dimension are returned as an array. Like, for the above example the dimension is:

[3, 2]

编辑:
有没有一种方法来创建这个信息的实际的数组?维[3,2]仅仅是一个例子。我可以具有[3,2,4,5]为好。可以的阵列从运行期间这个信息产生的?我没有在编译时这些信息。

Is there a way to create an actual array from this information? The dimension [3, 2] is just an example. I can have [3, 2, 4, 5] as well. Can an array be generated from this information during the runtime? I do not have this information during compile time.

有一些问题,我不能回答评论。所以,我在这里编辑

There is some problem, I cannot comment on answers. So, I am editing here.

推荐答案

如果你正在做数字工作,那么你应该使用一个图书馆。例如我的开源库 Vectorz 提供了多维数组/矩阵适当的支持( S)。

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).

然后,你可以这样做:

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

该INDArray对象封装所有的多面性,所以你不必担心写大嵌套循环所有的时间,再加上​​它提供了大量的正常Java数组没有其他的功能和能力。

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.

这篇关于创建运行在Java中的n维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆