简体   繁体   English

Java 3d Arraylist转换为3d阵列

[英]Java 3d Arraylist into a 3d Array

How would I transform a 3 dimensional ArrayList into a 3 dimensional array? 如何将3维ArrayList转换为3维数组? I was reading this post about turning a 2-D ArrayList into a 2-D array and I wondered how to extend the answer to 3-D. 我读这个岗位约转动2 d ArrayList到2 d阵,我不知道如何回答延长至3 d。

This is the fantastic thing with the new functional interface: 使用新的功能接口,这真是太棒了:

String[][][] stringArray = mainList.stream().map(u1 -> u1.stream().map(u2 -> u2.toArray(new String[0])).toArray(String[][]::new)).toArray(String[][][]::new);

would probably work. 可能会工作。 Sadly, I have currently no access to a JRE 8 to test it, but it should chain nicely. 遗憾的是,我目前无法访问JRE 8进行测试,但它应该可以很好地链接。

Simply map an additional time to propagate the arrays outward. 只需映射额外的时间即可向外传播阵列。

This works so well thanks to the way arrays works in java. 这要归功于数组在Java中的工作方式。 An int[][] array is actually an array of int[] arrays. 一个int[][]数组实际上是一个int[]数组。 If arrays were declared with the generics notation, this would mean that an integer array with two dimensions would be of type Array<Array<Integer>> , and so on. 如果使用通用符号声明数组,则这意味着具有二维的整数数组的类型将为Array<Array<Integer>> ,依此类推。 What the map functions does is simply utilizing this fact, and maps the ArrayList<> objects into these " Array<> " objects. 映射函数的作用只是利用这一事实,并将ArrayList<>对象映射到这些“ Array<> ”对象中。

We simply have to do it the correct number of times. 我们只需要做正确的次数。

This is based on the answer of the original post you refer to. 这是基于您引用的原始帖子的答案。 I just extended it to three dimensions: 我只是将其扩展到三个维度:

public static void main (String[] args) {
  ArrayList<ArrayList<ArrayList<String>>> input;
  String[][][]                            output;
  String[][]                              tmp;
  ArrayList<ArrayList<String>>            lvl2;
  ArrayList<String>                       lvl3;

  input = new ArrayList<ArrayList<ArrayList<String>>>();
  input.add(new ArrayList<ArrayList<String>>());
  input.get(0).add(new ArrayList<String>());
  input.get(0).get(0).add("foobar");

  output = new String[input.size()][][];
  for (int outer = 0; outer < input.size(); ++outer) {
    lvl2 = input.get(outer);
    tmp  = new String[lvl2.size()][];
    for (int inner = 0; inner < lvl2.size(); ++inner) {
      lvl3       = lvl2.get(inner);
      tmp[inner] = lvl3.toArray(new String[lvl3.size()]);
    }
    output[outer] = tmp;
  }
}

if you arraylist is like: A{B{C}} where: 如果您的arraylist类似于:A {B {C}}其中:

A1 = {
    B1={C1, C2, C3, ..., Cx}, 
    B2={C1, C2, C3, ..., Cy},
    ...
    Bm1={C1, C2, C3, ..., Cz}
A2 = {
    B1={C1, C2, C3, ..., Cx}, 
    B2={C1, C2, C3, ..., Cy},
    ...
    Bm2={C1, C2, C3, ..., Cz}
...
An = {
    B1={C1, C2, C3, ..., Cx}, 
    B2={C1, C2, C3, ..., Cy},
    ...
    Bmn={C1, C2, C3, ..., Cz}

to convert this arrayList to 3d array first get the proper lengths of that array 将此arrayList转换为3d数组,首先获取该数组的正确长度

for dimension 1 proper length is size of ArrayList A => n. 对于维度1,适当的长度是ArrayList A => n的大小。

for dimension 2 proper length is the max length of all B => max(m) 对于维度2,适当的长度是所有B的最大长度=> max(m)

for dimension 3 proper length is the max length of all C => max (x,y,z) 对于维度3,适当的长度是所有C的最大长度=> max(x,y,z)

then you can fill your array by looping through the ArrayList. 那么您可以通过遍历ArrayList来填充数组。

Unless if you are using Java8, there are probably more fancy ways to do it. 除非您使用的是Java8,否则可能会有更多奇特的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM