【问题标题】:Add depth to a Java Array Container为 Java 数组容器增加深度
【发布时间】:2017-08-07 11:38:35
【问题描述】:

我有一个 python 程序,它返回 shape (1, 1, 1, N) 列表。我在java中调用这个程序,它首先需要我初始化这个形状的容器(java数组)。如果容器被正确分配,python 函数可以将值转储到 java 数组中。 我想知道如何初始化这样一个容器,我几乎没有 Java 经验,但必须这样做才能进行一些演示。

编辑:

更多见解:

python返回的数组是这样的-

[[[[1,2,3,4,....N]]]]

我想要一种将这个数组接收到 java 数组中的方法。

【问题讨论】:

  • 你是指how to declare and initialize 4 dimensional array in Java?
  • 有关数组的详细信息,请参阅编辑。

标签: java python arrays arraylist


【解决方案1】:

我认为python中的shape(1, 1, 1, N)不等于java中的[[[[1,2,3,4,....N]]]]

你能详细描述一下python中的shape吗?是元组还是自定义类?

【讨论】:

  • 我提到的例子 [[[[1,2,3,4,....N]]]] ,这就是python向我展示的。这种类型的数组必须在 Java 中接收。
  • 我假设它是嵌入在三个数组中的单个数组(最内层)。一种表示它的方式是 List 接口,如下面的List<List<List<List<Integer>>>> = new ArrayList<>();
  • 你能举个合适的例子吗?
  • 先构造最内层列表(这里是a),然后将其添加到父列表中,如下所示,List<Integer> a = Arrays.asList(1,2,3,4); List<List<Integer>> b = new ArrayList<>(); b.add(a); List<List<List<Integer>>> c = new ArrayList<>(); c.add(b); List<List<List<List<Integer>>>> shape = new ArrayList<>(); shape.add(c); 现在形状列表将具有与 [[[[1, 2,3..]]]]。而且,要访问最里面的列表,您必须执行shape.get(0).get(0).get(0),这将具有a 列表内容。尝试使用System.out.println(shape); 来可视化数组
猜你喜欢
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2015-12-19
  • 2011-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多